I was trying to iterate following JSON object. when i try to access the content-items using page.content-items getting error.Is it not possible to access an object which have a key with "-"?.Please give a solution how to access this object.
{
"page": {
"title": "Romantic Comedy",
"total-content-items" : "54",
"page-num-requested" : "1",
"page-size-requested" : "20",
"page-size-returned" : "20",
"content-items": {
"content": [
{
"name": "The Birds",
"poster-image": "poster1.jpg"
},
{
"name": "Rear Window",
"poster-image": "poster2.jpg"
},
{
"name": "Family Pot",
"poster-image": "poster3.jpg"
}
]
}
}
}
It is possible to access the key with the - character, but not using dot notation. You need to use brakcet notation:
page['content-items']
to access the property.
Just use string value like in arrays:
page['content-items']
Use page['content-items'] because content-item is not a valid JavaScript identifier.
Related
I have the below code structure:
"name": {
"age": [
{
"data": {
"how old": "23"
}
},
my JSON key having space. How to access the property in an Angular template? I was doing the below but it's giving an error.
<span>{{age.data.'how old'}}</span>
{{age.data['how old']}}
or, probably
{{name.age[0].data['how old']}}
I have a case that is how to find the value of a key that is in sekrip as follows:
JSON.parse({
"data": [
{
"id_user": "351023",
"name": "",
"age": "29",
"link": "http://domain.com"
}
]
});
The above data was obtained from:
<script type='text/javascript' src='http://domain.com/target.php'></script>
I want to get the value of the key "id_user", anyone can help me?
Thanks before.
Several issues here. Firstly, the method you're looking for is JSON.parse(), not parseJSON. Secondly, what you're providing to that function is already an object, not a JSON string, therefore it doesn't need to be deserialised as you can access it as you would any normal object:
var obj = {
"data": [{
"id_user": "351023",
"name": "",
"age": "29",
"link": "http://domain.com"
}]
}
console.log(obj.data[0].id_user);
First its JSON.parse. Second it needs to be a JSON string.
Fiddle: http://jsfiddle.net/5m5qs1x4/
var jsonData = JSON.parse('{"data": [{"id_user": "351023","name": "","age": "29","link": "http://domain.com"}]}');
document.getElementById('test').textContent = jsonData.data[0].id_user;
You need to parse JSON string and than, access to keys/index
var data = JSON.parse("{\"data\": [{ \"id_user\": \"351023\",\"name\": \"\",\"age\": \"29\",\"link\": \"http://domain.com\"}]}");
document.write(data["data"][0]["id_user"]);
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 8 years ago.
i'm trying to pull some data from last.fm.
i get the following reply:
{
"tracks": {
"track": [
{
"name": "Once Upon a Dream",
"duration": "203",
"loves": "738",
"mbid": "92078817-2e04-4bcd-9c43-ebb9c2d1823c",
"url": "http://www.last.fm/music/Lana+Del+Rey/_/Once+Upon+a+Dream",
"streamable": {
"#text": "0",
"fulltrack": "0"
},
"artist": {
"name": "Lana Del Rey",
"mbid": "b7539c32-53e7-4908-bda3-81449c367da6",
"url": "http://www.last.fm/music/Lana+Del+Rey"
},
"image": [
{
"#text": "http://userserve-ak.last.fm/serve/34s/96432461.png",
"size": "small"
},
{
"#text": "http://userserve-ak.last.fm/serve/64s/96432461.png",
"size": "medium"
},
{
"#text": "http://userserve-ak.last.fm/serve/126/96432461.png",
"size": "large"
},
{
"#text": "http://userserve-ak.last.fm/serve/300x300/96432461.png",
"size": "extralarge"
}
]
}
And so on...
The problem lies when trying to access the image portion of the reply, the image object seems to have #text as variable name of the info i'm trying to access. a normal response.tracks.track[i].image[0].text obviously doesn't work.
is there some special way to access this variable ?
You can use the square bracket notation to access that variable like so:
response.tracks.track[i].image[0]['#text']
It's just a key name inside of the object. You cannot access it via dot notation since it contains an invalid characters but you can use bracket notation. Here's a really simple example demonstrating this.
var foo = {
'#bar': 'http://www.google.com/'
}
foo['#bar'] // will return a string value of http://www.google.com/
If your key contains any invalid characters it must be encased in a string. In your case, you're getting a JSON response which always contain string-encased key names.
Hope this helps!
This is my sample JSON file , which im trying to parse and read the values ....
C = {{
"Travel": {
"ServiceProvider": {
"Name": "SRS",
"Rating": "3 stars",
"Rates": "Nominal",
"Features": {
"OnlineBooking": "Yes",
"SMS_Ticket": "No"
},
"UserDetails": {
"Name": "Jack",
"Age": "33",
"Gender": "Male"
}
},
"BusProvider": {
"Name": "SRS",
"Rating": "3 stars",
"Rates": "Nominal",
"Features": {
"OnlineBooking": "Yes",
"SMS_Ticket": "No"
},
"UserDetails": {
"Name": "Jack",
"Age": "33",
"Gender": "Male"
}
}
}
}
I'm pretty new to JS , and i need to access the nested elements in a generic fashion.
Im not able to extract the details properly. Im getting stuck accessing nested the child elements.
The problem for me is that i wont always know the names of the "key's' to acess them , the JSON will be dynamic , hence i need a generic mechanism to acess the nested child elements. The Nesting can go upto 3 -4 levels.
what notation do we use to access the key / value pairs when the nesting is deep.
Any Help would be appreciated.
ater desirializing your object you can do this
var resultJSON = '{"name":"ricardo","age":"23"}';
var result = $.parseJSON(resultJSON);
$.each(result, function(k, v) {
//display the key
alert(k + ' is the key)
}
you can do it using recursively offcourse like this - Link Here
the way is the same just adapt to your example
For dynamic access you can use brackets notation i.e. var json = {nonKnown: 1}; now you can access it like that:
var unknowPropertyName = "nonKnown";
var value = json[unknownPropertyName];
But if you can not even define dynamically name of the property, then you should use
for(variableName in json){
if(json.hasOwnProperty(variableName)){
console.log(variableName);
}
}
You should get the basic idea from this. Good luck
Is it possible to assign references to objects in JSON? I have data that looks like this:
[{
name:"name",
Parent:[{
name:"parentName"
Parent:[{
.....//and so on
}]
}]
}]
I need to traverse it in JavaScript and also change the person's name. How can I do this?
Old question but some possibly new answers like JSON Spec and JSON Reference https://json-spec.readthedocs.io/reference.html
[{
"name": "John",
},
{
"name" : "Jack",
"parent": {"$ref": "#/0"}
},
...
]
or possibly better with JSON Path syntax http://goessner.net/articles/JsonPath/
[{
"name": "John",
},
{
"name" : "Jack",
"parent": {"$ref": "$.[?(#.name=='John')]"}
},
...
]
You can't. You can specify the path to the parent as a string and evaluate that at runtime, but as JSON is just strings, integers, arrays, and dictionaries, you can't use references.