Reputation: 1002
I am interested in storing key-value pair of metadata inside a JSON array containing multiple JSON objects. This will instruct a generic parser what to do with the list of JSON objects in the JSON Array when it is processing the JSON Array. Below is a sample JSON, with where I am hoping to have some sort of metadata field.
{
"Data": [
<< "metadata":"instructions" here >>
{
"foo": 1,
"bar": "barString"
},
{
"foo": 3,
"bar": "fooString"
}
]
}
What is the proper way to structure this mixed data JSON array?
Upvotes: 0
Views: 7360
Reputation: 2289
If you can modify the structure of the data, why not add a property meta
with your instructions (i.e. Data.meta
) and another property content
(for want of a better word...) (i.e. Data.content
), where the latter is the original array of objects.
That way, it is still valid JSON, and other implementations can read the meta-field as well without much ado.
Edit: just realized, you would also have to make Data
an object rather than array. Then your JSON-schema should become this:
{
"Data": {
"metadata": "instructions here",
"content": [
{
"foo": 1,
"bar": "barString"
},
{
"foo": 3,
"bar": "fooString"
}
]
}
}
This will probably be the most stable, maintainable and portable solution. For refrence, something similar has already been asked before.
Upvotes: 1
Reputation: 1002
After some additional discussion with another developer, we thought of one way to include the metadata instructions in the data
JSON array.
{
"Data": [
{
"metadata": "Instructions"
}
{
"foo": 1,
"bar": "barString"
},
{
"foo": 3,
"bar": "fooString"
}
]
}
This approach does come with the limitation that index 0 of the data
JSON array MUST contain a JSON Object containing the metadata
and associated instructions for the generic parser. Failure to include this metadata object as index 0 would trigger an error case that the generic parser would need to handle. So it does have its trade-offs.
Upvotes: 0
Reputation: 1
I will go to try help you..
"metadata" : [ { "foo": 1, "bar": "barString" }, { "foo": 3, "bar": "fooString" } ]
Upvotes: -1
Reputation: 6348
I would add a meta
key as a peer of data like below. This would separate your data from the meta data.
{
"Meta": {
"metadata":"instructions"
},
"Data": [
{
"foo": 1,
"bar": "barString"
},
{
"foo": 3,
"bar": "fooString"
}
]
}
Upvotes: 1