Storing Array in JSON

A valid JSON object always starts with { and ends with }, and an array is enclosed within [ and ]. Look at http://json.org.

If it is just an array you want to store and not store a name attribute to it. You can just store it as [1,2,3,4,5,6]. For example if you are storing it in an RDBMS, you may name the column as array and store the value as a JSON array.

In case you want to preserve the name of the attribute, or possibly want to have more attributes, you got to use this format: {"array" : [1,2,3,4,5,6]}


The accepted answer is wrong. JSON can start and end with an array. The official JSON document says

JSON is built on two structures:

  1. A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

You can also see it through JSON validator.

In a nutshell, while you still cannot do "array" : [1,2,3,4,5,6], you can store it like [1,2,3,4,5,6].


No, that is the way the JSON is formatted

The opening/closing { } are saying this is in json (I guess kind of like <html></html>)

There is no reason you can't do

{ "array" : [1,2,3,4,5,6] }

What this is saying is that there is one field called array which contains an array of numbers

Tags:

Java

Json