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 Overflow
🌐
Readthedocs
json-spec.readthedocs.io › reference.html
JSON Reference — JSON Spec documentation - Read the Docs
For example, your document refer to stored documents on your filesystem: from jsonspec.reference import Registry from jsonspec.reference.providers import FileSystemProvider obj = { 'foo': {'$ref': 'my:doc#/sub'} } provider = FileSystemProvider('/path/to/my/doc', prefix='my:doc') resolve(obj, '#/foo/2', { 'obj2': {'sub': 'quux'} })
Discussions

How to refer to variables in a JSON configuration file
When I try, it just passes the string module.nsg.security_groups.id I also tried ${module.nsg.security_groupds.id) · Hi @brandootr! You bumped a very old topic and so I’ve moved your question into a separate topic so we can discuss it without creating notification noise for those who ... More on discuss.hashicorp.com
🌐 discuss.hashicorp.com
5
0
January 13, 2022
Reference JSON variable with string acquired from other JSON
I have 2 JSON files. ... What I am stuck on is that I need to, in one javascript file, get the tag from file 2 and use it to reference the variable in file 1. More on stackoverflow.com
🌐 stackoverflow.com
design - How to represent object references in JSON? - Software Engineering Stack Exchange
I am trying to figure out what's the best approach when dealing with object references in a JSON to be sent to my server for deserialization. To clarify, what I mean is how to refer to data contai... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
June 10, 2016
Embed variables in JSON file
JSON by itself cannot do that. That being said, there's nothing saying that in whatever language you are using can convert the "key" folder_name into ${folder_name} for a "find-and-replace" later. Conversion and replacement of keys with "variables" inside, like path would have to occur after ... More on reddit.com
🌐 r/learnprogramming
4
2
February 21, 2022
🌐
Opis
opis.io › json-schema › 2.x › variables.html
Variables ($vars) | Opis JSON Schema
In the above example, $vars contains ... using JSON pointers. To do this, you will reference a value in the same way you reference other schemas: by using $ref keyword....
🌐
HashiCorp Discuss
discuss.hashicorp.com › terraform
How to refer to variables in a JSON configuration file - Terraform - HashiCorp Discuss
January 13, 2022 - When I try, it just passes the string module.nsg.security_groups.id I also tried ${module.nsg.security_groupds.id) · Hi @brandootr! You bumped a very old topic and so I’ve moved your question into a separate topic so we can discuss it without creating notification noise for those who ...
🌐
Redocly
redocly.com › docs › resources › ref-guide
How to use JSON references ($refs)
December 10, 2024 - Reference a definition within a file by remote URL and pointer to the object. Use a JSON Pointer to the object.
Top answer
1 of 3
9

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.

2 of 3
4

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).

🌐
Reddit
reddit.com › r/learnprogramming › embed variables in json file
r/learnprogramming on Reddit: Embed variables in JSON file
February 21, 2022 - JSON by itself cannot do that. That being said, there's nothing saying that in whatever language you are using can convert the "key" folder_name into ${folder_name} for a "find-and-replace" later. Conversion and replacement of keys with "variables" inside, like path would have to occur after the variables without keys.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_json_syntax.asp
JSON Syntax
Temporal Study Path Temporal Intro Temporal vs Date Temporal Duration Temporal Instant Temporal PlainDateTime Temporal PlainDate Temporal PlainYearMonth Temporal PlainMonthDay Temporal PlainTime Temporal ZonedDateTime Temporal Now Temporal Add/Subtract Temporal Since/Until Temporal Conversion Temporal Formats Temporal Mistakes Temporal Migrate Temporal Standards Temporal Reference
🌐
Niem
niem.github.io › json › reference › json-schema › references
JSON References | NIEM GitHub
In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. ... A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.
🌐
Microsoft Power Platform Community
powerusers.microsoft.com › t5 › General-Power-Automate › Reference-a-dynamic-JSON-element-with-a-variable › td-p › 1703888
Reference a dynamic JSON element with a variable
August 4, 2022 - Quickly search for answers, join discussions, post questions, and work smarter in your business applications by joining the Microsoft Dynamics 365 Community.
🌐
Stack Overflow
stackoverflow.com › questions › 28662795 › referencing-a-json-key-with-javascript-variable
Referencing a JSON key with Javascript variable - Stack Overflow
May 22, 2017 - How do I replace the "Argentina" key with a javascript variable string? jQuery(document).ready(function() { $.getJSON('countries.json', function(data) { var output= data.Argentina[0].countryPhone1; alert(output); }); }); Here's what my .json data file looks like: { "Argentina": [{ "countryName":"Argentina", "countryPhone1":"1234" }], "Brazil": [{ "countryName":"Brazil", "countryPhone1":"5678" }] } I tried applying the suggested solutions here, here and here, but I couldn't figure out a way to adapt them to my example.
🌐
W3Schools
w3schools.com › js › js_json_objects.asp
JSON Object Literals
The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › JSON
Working with JSON - Learn web development | MDN
After all that, the superHeroes variable will contain the JavaScript object based on the JSON. We are then passing that object to two function calls — the first one fills the <header> with the correct data, while the second one creates an information card for each hero on the team, and inserts it into the <section>. Now that we've retrieved the JSON data and converted it into a JavaScript object, let's make use of it by writing the two functions we referenced ...
🌐
Js
json-e.js.org › language.html
Language Reference - JSON-e
The context is an object, giving ... nested scopes. When an expression references a variable, evaluation looks for a variable of that name in all scopes, from innermost to outermost, and uses the first that it finds....
🌐
ServiceNow Community
servicenow.com › community › developer-forum › pass-variable-value-to-json-object › m-p › 1608224
Solved: Pass variable value to json object - ServiceNow Community
July 27, 2022 - var json = { "opened_by":"62826bf03710200044e0bfc8bcbe5df1", "requested_for": newReferent.toString(), "department":"221f3db5c6112284009f4becd3039cc9" }; Depending upon how and where you are assigning newReferent the type is getting skewed. ... The fields in the include script are returning empty. in Developer forum yesterday · Unexpected Behavior: Catalog Variable Showing Another User’s Phone Number in Developer forum yesterday
🌐
Google Cloud
googlecloudcommunity.com › google cloud › apigee › knowledge hub
How do I "easily" insert variables into JSON Payloads? - Knowledge Hub - Google Developer forums
April 3, 2015 - Often you get into a situation where within an API Proxy you may need to insert some variable data into a JSON payload that is needed by your back-end or target service. You can leverage the AssignMessage policy to do that. For eg: {"name":"foo", "type":"bar"} true