Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 503
Rajagopal 웃 Avatar asked May 21 '12 14:05

Rajagopal 웃


2 Answers

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')]"}
 }, 
...
]
like image 94
Vlad Avatar answered Oct 01 '22 05:10

Vlad


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.

like image 42
Cole Tobin Avatar answered Oct 01 '22 05:10

Cole Tobin