0

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?

Andrew
  • 902
  • 1
  • 11
  • 28

4 Answers4

1

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.

Oliver Baumann
  • 2,209
  • 1
  • 10
  • 26
1

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"
        }
    ]
}
jfadich
  • 6,110
  • 2
  • 20
  • 31
  • This is a valid approach. It would require creating a second field to store the metadata instructions outside of the `data` JSON array. It has the benefit of keeping the `data` JSON array in an unmodified state but the second field could be disadvantages depending on how the full JSON data was structured (The JSON I used in this question is a simplification of a larger JSON dataset). Take a look at the answer I made as an alternative, through it also has limitations. – Andrew Oct 25 '17 at 00:40
0

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.

Andrew
  • 902
  • 1
  • 11
  • 28
  • Why not make `Data` an object instead of an array, and set keys for metadata and your actual data-items, as has been suggested by myself and similarly jfadich? Is that not a viable approach? It would remove the 0-index constraint you mentioned. – Oliver Baumann Oct 25 '17 at 10:56
-1

I will go to try help you..

"metadata" : [ { "foo": 1, "bar": "barString" }, { "foo": 3, "bar": "fooString" } ]

  • sorry, because my code is only in one line.. hope that i help to solve your problem :) – L.Cesca Oct 25 '17 at 00:21
  • Not quite what I was aiming for. This is essentially renaming the field `data` as `metadata`. I was looking to include the `metadata` _inside_ the `data` JSON array. – Andrew Oct 25 '17 at 00:25