Accessing property with space in template expression - javascript

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']}}

Related

unable to access JSON child elements

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.

JSON with outer brackets in d3.js?

when i got a JSON file called "test.json" which looks like this:
[{
"name":"abc",
"parent":"null",
"children":[],
}
]
That means with the outer brackets [] wrapping the whole data, it is not possible to use it in d3.js with the hierarchy function:
d3.json("test.json", function(a) {
var hierarchy = d3.hierarchy(a),
links = hierarchy.links()
nodes = hierarchy.descendants();
Now, when i leave the brackets away, which means that "test.json" looks like this, it is possible to use the file:
{
"name":"abc",
"parent":"null",
"children":[],
}
Now, my question is: Is this a specific problem of d3.js in general, maybe with the combination of the hierarchy function or it is always not working, when wrap the whole file with brackets []?
The outer brackets signify an array. To get the object / map inside them, index it out:
a[0]
So if a is:
[ {
"name": "abc",
"parent": "null",
"children": []
}
]
Then a[0] is just
{
"name": "abc",
"parent": "null",
"children": []
}

Accessing JavaScript Sub-properties by Name

I wrote the following JavaScript function (part of a larger "class") to help ensure anybody using the object stores attribute values in the "values" property.
function _updateAttributes(attribute, value) {
_attributes[attribute] = { values: { value: value }};
}
It works fine for a flat structure, but falls apart when I start trying to use it for sub-properties.
After running the following code:
myEntity.updateAttribute('name', 'Frankenstein');
myEntity.updateAttribute('name.source', 'John Doe');
I'd like the following structure:
{
"attributes": {
"name": {
"values": {
"value": "Frankenstein"
},
"source": {
"values": {
"value": "JohnDoe"
}
}
}
}
}
Instead, it's coming out like this:
{
"attributes": {
"name": {
"values": {
"value": "Frankenstein"
}
},
"name.source": {
"values": {
"value": "JohnDoe"
}
}
}
}
Is there any clean way to write this JavaScript or will I be faced with splitting out the strings and manually building the structure?
NOTE: I realize even the preferred structure is a little odd, but there's a Java object I'm mapping to that expects this format, so I don't have any options here.
You'll have to parse the string (parse is a bit strong, just a single split('.') with a loop).
But frankly, the cleaner way would simply be:
myEntity.name = {values: 'Frankenstein'};
myEntity.name.source = {values: 'John Doe'};

Complex JSON string Parsing in JavaScript

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

Are references possible in JSON?

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.

Categories

Resources