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?

4 Answers 4

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"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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.

1 Comment

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

I will go to try help you..

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

2 Comments

sorry, because my code is only in one line.. hope that i help to solve your problem :)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.