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": {"
.[?(@.name=='John')]"}
},
...
]
Answer from Vlad on Stack OverflowVideos
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": {"
.[?(@.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.
That's not exactly very good JSON above there, in the case of the Agents value the second key will override the first.
You probably meant:
"Agents" : [
{"name" : "Bob Barker"},
{"name" : "Mona Mayflower"}
],
Then you'd access the first agent's name as
homes[0]['Agents'][0]['Name']
Similarly, to get one of the values from the Listings, you'd do something akin to:
homes[0]['Listings'][0]['city']
- or -
homes[0].Listings[0].city
The dot syntax can be used wherever there is a valid identifier, else you need to use the array syntax.
As a side note, I'm not sure the structure of the data, but it's possible that you can eliminate the outer-level [] that's enclosing your whole structure in an array. Then you wouldn't need to access everything as homes[0]['Listings'] and instead simply homes['Listings'].
Your JSON syntax is wrong. You can't have the same key twice in an object. Instead, you need an array:
var homes = {
"Agents" : [
{ "name" : "Bob Barker" },
{ "name" : "Mona Mayflower" }
],
...
}
Then you can access the agents like so:
homes.Agents[1] // => { "name": "Mona Mayflower" }
// or
homes.Agents[1].name // => "Mona Mayflower"
Hey peeps,
Okay, so, just playing with making a wee CCG and storing the stats in a JSON object, but some of the stats are self-referential and I was wondering if there was a way around this. FOR EXAMPLE :
var cardOne = {
name : "Example Card",
armour : 70,
courage : 100 + this.armour
};Now, I know that this.armour won't work in this context, but what would?
Any help would be grand. Thanks in advance!
-P
EDIT 1: okay, seeing as this is Chrome-only project I've decided to take advantage of ES6's class notation and implement it like this :
class Card {
constructor(_name, _armour){
name = _name;
armour = _armour;
courage = 100 + _armour;
}
}
var testCard = new Card("Example Card", 70);...and that's how it'll stay for now, but if you can point me towards a more efficient alternative then that'd be great!
EDIT 2: as u/talmobi/ pointed out, this is the equivalent of simply writing :
var Card = function(_name, _armour){
this.name = _name;
this.armour = _armour;
this.courage = 100 + _armour;
};
var testCard = new Card("Example Card", 70);Well...that's not exactly what they said, but you can read their full comment here.
No, JSON is essentially represented as a string. Nodes JSON.stringify will attempt to convert 100 + this.armour to a primitive value (in this case NaN), but other implementations of stringifywould perhaps render it as a single string that when parsed would result in a string "100 + this.armour".
If I were you, I would store the stringified object only with values that can be represented as primitive values. Then, when you parse the JSON string into a JS object, pass it into a function that will compute the additional properties that rely on other properties.
ES6 class notation is just sugar... why didn't you use a constructor function from the start?
var Card = function (o) {
// when using "new" the fn implicityly creates "this = {}";
Object.assign(this, o); // copy vals from argument
this.courage = 100 + this.armor;
// when using "new" the fn implicitly returns "this"
};
// call the Card fn as a "constructor" with "new"
var cardOne = new Card({name: "example", armor: 70});
console.log(cardOne);