Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show another object's value in another object, JSON File

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
     "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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading