Extracting key values across nested JSON [duplicate] - json

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]);
}

Related

Powershell: How can I dynamically iterate over nested json objects? [duplicate]

This question already has answers here:
PowerShell iterate through json of key value pairs
(1 answer)
Powershell Selecting NoteProperty Type Objects From Object
(2 answers)
Closed 3 months ago.
Suppose I have something like this:
"config_service": {
"config_service_1": {
"service": "__service1__"
},
"config_service_2": {
"service": "__service2__"
}
},
I am able to grab out the parent json by doing
$jsonMappings.config_service
But how can I dynamically iterate over each json value in there and do something like, for each object within config_service, extract value of key named 'service'? When I try doing ForEach-Object it treats the config service 1 and 2 as one big object.

from JSON object to Java Script Object that contains js function name [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 5 years ago.
I have this JS object:
{ validator: myValidator }
myValidator is a java JAVASCRIPT function NAME that will be declared somewhere else. I am planning to use it like:
<TableHeaderColumn dataField='status' editable={ { validator: myValidator } }>Job Status</TableHeaderColumn>
where TableHeaderColumn is a react component. So, the question is: What is the JSON string that after using JSON.parse or a similar command I will obtain the { validator: myValidator } object where myValidator is "the name of a function", not a string. This is not clear for me inclusive at the referenced solution.
To convert a JS Object to JSON, you can use json = JSON.stringify(jsObject)
To convert JSON to a JS Object, just use jsObject = JSON.parse(json)

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

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;

Get length/size of json data

I have use the following codes to get my json data with certain nodes.
console.log(Object.keys(data.Top["person"]).length);
It is work fine in following structure of json data array:
data:{"Top":{"person":[{"A":"a","B":"b"},{"A":"a","B":"b"}]}}
However when the person only have one nodes, it always show the answer 2 to me, it should be answer 1.
data:{"Top":{"person":{"A":"a","B":"b"}}}
Is it possible to solve this error?
length property is supported by type array.
data:{"Top":{"person":[{"A":"a","B":"b"},{"A":"a","B":"b"}]}} in case of this person is array enclosed with [ ]
Where as for data:{"Top":{"person":{"A":"a","B":"b"}}} person is just an object. Hence length is undefined for it.
If you are creating json out of string or formatting it make sure to include [ and ] for person attribute.
JSFiddle
https://jsfiddle.net/d6pqnckh/
Also use JSON formatter to test JSON structure.
UPDATE
Since you are not sure of JSON structure. What you could do is before accessing length of person check if it is an array or object.
if(Object.prototype.toString.call(data.Top.person) === '[object Array]')
alert(data.Top.person.length);
//It is an array
else
alert("I AM OBJECT"); //It is of an object type
Updated Fiddle: https://jsfiddle.net/wys7awuz/
To make it an array regardless https://jsfiddle.net/ofkboh6k/
var p = data.Top.person;
delete data.Top.person;
data.Top.person = [];
data.Top.person.push(p);
alert(data.Top.person.length);
Include this in else part of condition. It will make it an array.
length works for type array.
change your JSON to
data:{"Top":{"person":[{"A":"a","B":"b"}]}}

Get value of json string

[
{
"type": "spline",
"name": "W dor\u0119czeniu",
"color": "rgba(128,179,236,1)",
"mystring": 599,
"data": ...
}
]
I am trying to access this json as json['W doręczeniu']['mysting'], and I get no value why is that?
You're trying to access the index "W doręczeniu" but that's not an index it's a value. Also, what you seem to have is an array of JSON objects.
The [ at the start marks the array, the first element of which is your JSON object. The JSON obj begins with the {
You're also trying to use a [ ], but JSON values are accessed with the dot operator.
I'm not sure which index you're actually trying to access, but try something like this:
var x = json[0].mystring;
The value of "W doręczeniu" is not a key, so you cannot use it to get a value. Since your json string is an array you'll have to do json[0].nameto access the first (and only) element in the array, which happens to be the object. Of course, this is assuming json is the variable you store the array into.
var json = [{"type":"spline","name":"W dor\u0119czeniu","color":"rgba(128,179,236,1)","mystring":599}];
console.log(json[0].mystring); //should give you what you want.
EDIT:
To get the last element in a js array, you can simply do this:
console.log( json[json.length -1].mystring ); // same output as the previous example
'length - 1' because js arrays are indexed at 0. There's probably a million and one ways to dynamically get the array element you want, which are out of the scope of this question.