How to get value from specific key in NodeJS JSON [duplicate] - json

This question already has an answer here:
Getting value from object in javascript
(1 answer)
Closed 6 years ago.
{
"id": 83,
"key": "hello",
"en": "Hello",
"mm": "This is greeting",
"created_at": "2016-12-05T10:14:02.928Z",
"updated_at": "2017-01-31T02:57:11.181Z"
}
I'm trying to get value from mm key in NodeJS. I've tried to get translation["mm"] but it does not work at all. Please help me how to get mm from above data.
My current node version is 5.11

Edited The Answer. Now Its working for above object in question
You can use following function to access the keys of JSON. I have returned 'mm' key specifically.
function jsonParser(stringValue) {
var string = JSON.stringify(stringValue);
var objectValue = JSON.parse(string);
return objectValue['mm'];
}

JSON is an interchange format, meaning it's only a text representing data in a format that many applications can read and translate into it's own language.
Therefore, using node.js you should parse it first (translate it to a javascript object) and store the result in a variable:
var obj = JSON.parse(yourJSONString);
Then, you can ask for any of it properties:
var mm = obj.mm;

Related

How to get the key name in json?

My previous problem was I'm unable to arrange the json structure like what I wanted. And I found some answers that looks like it almost satisfy my needs but unfortunately I don't know if it's working or not because another problem has occurred.
Below, I arranged my own json data based on the json structure by someone named Programmer.
{
"dialog_type": {"human": {"inner": "He is so scary"}}
}
Here, I have a key called "human". I have two keys in my data. First is "human" and second is "non_human". Now if I have two data in my json file, it will become like this :
{
"dialog_type": {"human": {"inner": "He is so scary"}}
},
{
"dialog_type": {"non_human": "Once upon a time..."}
}
This case is maybe simillar to someone asked here. But unfortunately I have no idea if it's possible to do that in unity. I want to make a method like this answer. So I can determine what action to take by comparing those keys.
Now the question is how do I get the key name as a string in my json data using C# ?
To access the property names of a Unity javascript object, you can use:
for(var property in obj) {}
For instance, this will log all keys (i.e. property names) of all the property key-value pairs in a Unity javascript object (e.g. "key1" and "key2"):
function Start () {
var testObject = {
"key1": "value 1",
"key2": "value 2"
};
for(var property in testObject) {
Debug.Log(property.Key);
};
}
That should give you a way to check objects for any matching property names you are interested in.

Single value from json array using nodejs

so i have this data which is outputted from stringify(doc)
{
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
}
but im not sure how can i go ahead in getting a single value from it, e.g message. Any help would be appreciated as ive tried different methods and they seem to not be working
After you use stringify method the value you have is string.
So You can not get single property from it, except you convert it become object again and get property it. Like this:
object = {
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
};
// JSON string is value which you get after use Stringify method
jsonString = JSON.stringify(object);
// Convert jsonString to object again and get message property
message = JSON.parse(jsonString).message
stringify() will convert json to string.
Before running stringify(), doc is already json and you can get message by doc.message.
after running stringify(), the result will be string, if you want to get json back, you can use JSON.parse()

Extracting key values across nested JSON [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I am trying to extract the values from a nested JSON file that looks like so:
var mymenu = {"menu": [{"page": {"url": "http://foo.bar.com","random stuff": {"junk": "rubbish"}}},{"page": {"feed": "http://foo.bar.com"}},{"menu": [{"submenu": [{"page": {"feed": "http://foo.bar.com"}}]}]}]};
The keys I am trying to extract are the feeds with contain urls. I have tried a for...in loop into the retrieved JSON but I can only get as far as pulling the object that the feed is in resulting in the stringified object as a whole. Is there a way to get just the keys I need from the JSON file?
I just validated your json on JSONLint and it seems to be invalid.
Parse error on line 7:
... } }
----------------------^
Expecting 'STRING'
Try this json, and remember to assign a name to your variable.
var mymenu = {"menu": [{"page": {"url": "http://foo.bar.com","random stuff": {"junk": "rubbish"}}},{"page": {"feed": "http://foo.bar.com"}},{"menu": [{"submenu": [{"page": {"feed": "http://foo.bar.com"}}]}]}]};
Next iterating through the json isn't too bad, just think of it as a multi dimensional array with key value pairs. For instance, if you're running firefox it's a good idea to get firebug.
for(var i = 0; i < mymenu['menu'].length; i)
{
console.log(mymenu['menu'][i]);
}

How to grab data from JSON in CoffeeScript? [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
How to extract specific data from JSON using CoffeeScript?
I want to grab a specific piece of data from a massive JSON string. The entire string would be more than 10 pages long if posted here, so I'm just including an example snippet:
{ name: '',
keys:
[ 'statType',
'count',
'dataVersion',
'value',
'championId',
'futureData' ],
object:
{ statType: 'TOTAL_SESSIONS_PLAYED',
count: { value: 5 },
dataVersion: 0,
value: { value: 5 },
championId: { value: 31 },
futureData: null },
encoding: 0 }
How can I use CoffeeScript to:
parse that string to locate the object with a specific value, such as TOTAL_SESSIONS_PLAYED,
take the numerical value from that object (the value field), and
ideally, append that value into an external text file?
I am pretty much a super noob programmer. Basically, how could I, in this example, take that 5 value from the object labelled TOTAL_SESSIONS_PLAYED, and append it into a text file using CoffeeScript?
Whether you're doing this in the browser or in Node, you should be able to pass the JSON string to JSON.parse and pick out the value you want. You can then append to a file using Node's fs module like this: https://stackoverflow.com/a/11267583/659910.
fs = require 'fs'
# Sample JSON string.
json = '{ "statType": "TOTAL_SESSIONS_PLAYED", "count": { "value": 5 }, "dataVersion": 0 }'
data = JSON.parse(json)
fs.appendFile('/tmp/data.txt', data.count.value, (error) -> throw error if error)

Retrieving References data from JSON

I have json data in the following format
{"updates":
{"message" :"[[student:123]] is present."},
"references":[{"type":"student","full_name":"XYZ","id":123}]
}
How can I map the student name to the message using the id present over the message?
I am relatively new to JSON parsing. I am currently using EJS template to manipulate the JSON into HTML.
In that just using
<%alert(updates.message.student)%>
returns "undefined". Please help.
updates.message is a string, not a JavaScript object. You can tell by the quotes around the whole attribute. JavaScript strings don't have a student property, so you are getting undefined. You can parse out the JSON part from the string with regular expressions and then use JSON.parse() to get the JSON object. However, the student id is also in updates.references[0].id in your example.
To get the student ID, do this:
<% alert(updates.references[0].id) %>
edit: If you are really want to get the id out of the message, you need to parse it out somehow. If the message format will always be the same, you can try a regular expression or string splitting to get the part containing the id.
var id_part = json.updates.message.split(" ")[0];
//parse out just the ID number in a group
var re = /\[\[[^:]+:(\d+)\]\]/;
var matches = re.exec(id_part);
var id = matches[1];
To then get the corresponding data out of the references part, you need to loop through until you find one with the id from the message. This would work.
//Ghetto old for loop for browser compatibility
for (var i = 0; i < updates.references.length; i++) {
if (updates.references[i].id == id) {
//We have found the reference we want.
//Do stuff with that reference.
break;
}
}
try
var json = {
"updates": {
"message": "[[student:123]] is present."
},
"references": [
{
"type": "student",
"full_name": "XYZ",
"id": 123
}
]
};
alert(json.references[0].full_name);