Home > Software engineering >  Show another object's value in another object, JSON File
Show another object's value in another object, JSON File

Time:12-22

There must be a way around this but I don't remember.

I need to read the value of another object in the same json file

For ex:

{
     "firstName": "John",
     "lastName": "Smith",
     "fullName" : "{firstName} {lastName}" // I need to show two values here, how can I do that?
}

I don't need any js functions just this should be inside the JSON file

CodePudding user response:

JSON is a fairly simple data format. It does not have any features that allow one property to reference anything.

You could build your own syntax layer on top of JSON and do additional processing. Perhaps using a reviver function or by post-processing the parsed object. The latter would likely be easier as you wouldn't have the risk of trying to reference a property that hasn't been parsed yet.

You could also look for an existing data format which suits your needs better than JSON. OmegaConf supports variable interpolation and is based on YML (which fulfils a similar niche to JSON) although it is a Python library rather than a JS one. There may be an existing JS library that suits your needs.

  • Related