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 OverflowOld 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.
How to refer to variables in a JSON configuration file
Reference JSON variable with string acquired from other JSON
design - How to represent object references in JSON? - Software Engineering Stack Exchange
Embed variables in JSON file
Did you mean something like this ?
var file1 = {
object: {
variable: true
}
};
var file2 = {
object2: {
tag: "object.variable"
}
};
var result = file2.object2.tag.split('.').reduce((a, b) => a ? a[b] : null, file1);
console.log(result);
This could be the possible solution :
var query = {
object2: {
tag: "data.object.variable"
}
}
var data = {
object: {
variable: true
}
}
console.log(eval(query.object2.tag));
For inspiration, you may want to look into the way some of the json based api's (ex: json api, HAL) handle embedding.
One simple answer is to track your data in a key value store. For example
{ "/players/0" : {...}
, "/players/1" : {...}
, "/players/2" : {...}
, "/players/3" : {...}
, "/teams/0" : {...}
, "/teams/1" : {...}
}
And then you describe the players assigned to your team using local references
, "/teams/0" :
{ refs :
[ "/players/0"
, "/players/1"
]
}
As it happens, this scheme covers the case where you have identifiers too. Or where you only have some identifiers
, "/teams/0" :
{ refs :
[ "/players/0"
, "/players/2ad8cabe-2f93-11e6-ac61-9e71128cae77"
]
}
There are fancier versions of this idea (see the links).
That said, I've been down this road myself, and I really tied myself in knots until I concluded: if what you really have is a list of names, rather than a list of players, admit that to yourself, code it that way, and deal with it. It's the more honest way of representing what's going on in the domain at that point in time.
In which case, the payload of your message should look very close to:
{ "Team 1" :
[ "Player 1"
, "Player 2"
]
, "Team 2" :
[ "Player 3"
, "Player 4"
]
}
If that makes you twitchy, remember: this isn't a description of your domain objects; it's a message. The changes it makes to your domain are a side effect. Jim Webber covers this in the introduction to this talk.
This is a really nice question.
The problem arises because you are modeling redundant information and try to avoid redundancy at the same time.
On the one hand, you have a collection of players
players = [{"id":"1"},{"id":"2"},{"id":"3"}]
On the other hand, you have a colletion of teams, which itself consist of subsets from players.
teams = [ {"id":"1", "players": [ players[0], players[1] ]} ]
This gives a composition:
players = [{id:1},{id:2},{id:3},{id:4}]
teams =[ {id:1, players:[players[0], players[1]]} ]
data = {players:players, teams:teams}
Look here for the Fiddle and watch the result.
As you see, the references cause redundant information in JSON.stringify, because you have redundant information in your data object.
The problem of avoiding redundancy arises when sending data to the server.
Take a step back.
What do you want to tell the server?
a) Here you have a list of teams, please persist it for me. I come back to you later. Oh, by the way, the teams contain the following players blablabla
b) Here you have a list of players. Keep 'em safe for me. I need them later to build `teams.
Your model shows, that you are not clear.
There are several usecases:
I) I want to create new players
IIa) I want to create new teams
IIb) I want to put players in teams
I) In a REST-context, you could issue a POST to /players.
IIa,b) You POST to /teams your collection of teams.
How to deal with the situation, that you want to save requests and do not issue a single POST for each creation of a new player (and an additional one for submitting the team)?
I would go for the following:
You have a collection of players: some of them have an id, indicating, that they were already persisted; some of them don't.
If you create teams, you issue only one POST request with the teams, containing the full player objects.
[{"name":"team1", "players":[{"id":"1", "name":"player1"}, "name":"player2"}]}, ... ] // you get the idea
The server isn't interested in knowing explicitely how many players there are: it is implicitely clear: it's the sum of all players (which might be the sum of all players in all teams).
The server has to figure out, how to persist the players and how to set foreign keys (in case of relational DBs).
Use the other notation var a = data['243232'].id
Remember all objects in JS are really just associative arrays.
Object keys just a variable in js and thus require proper naming
the variable naming rules are.
- The first character must be a letter (either uppercase or lowercase) or an underscore (_), or a dollar sign ($).
- Subsequent characters can be letters, numbers, underscores, or dollar signs in JavaScript Variables.
- The JavaScript Variable name can't be a reserved word of JavaScript, see details of JavaScript Reserved Characters
JSON normally uses an eval() function to turn the string into a data-structure. This allows for incorrect keys. If you want to reference an improper key, you need to use the associative array method.
As for you addition
var c = 243232;
var d = data[c].id;
alert(d) //it gives as undefined.
Will work
Use data[c].id.
In JavaScript, .prop is syntactic sugar for ["prop"]. The bracket notation allows you to use values which would be invalid when using . (such as background-image) and variables.
var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}],
x = "ADAM";
alert(data[0][x].TEST);
http://jsfiddle.net/n0nick/UWR9y/
Since objects in javascripts are handled just like hashmaps (or associative arrays) you can just do data['adam'].TEST just like you could do data.adam.TEST. If you have a variable index, just go with the [] notation.
var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}]
alert(data[0].ADAM.TEST);
alert(data[0]['ADAM'].TEST)
if you just do
var data = {"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}};
you could access it using data.ADAM.TEST and data['ADAM'].TEST