241

This is all you need for valid JSON, right?

["somestring1", "somestring2"]
finneycanhelp
  • 9,018
  • 12
  • 53
  • 77

4 Answers4

376

I'll elaborate a bit more on ChrisR awesome answer and bring images from his awesome reference.

A valid JSON always starts with either curly braces { or square brackets [, nothing else.

{ will start an object:

left brace followed by a key string (a name that can't be repeated, in quotes), colon and a value (valid types shown below), followed by an optional comma to add more pairs of string and value at will and finished with a right brace

{ "key": value, "another key": value }

Hint: although javascript accepts single quotes ', JSON only takes double ones ".

[ will start an array:

left bracket followed by value, optional comma to add more value at will and finished with a right bracket

[value, value]

Hint: spaces among elements are always ignored by any JSON parser.

And value is an object, array, string, number, bool or null:

Image showing the 6 types a JSON value can be: string, number, JSON object, Array/list, boolean, and null

So yeah, ["a", "b"] is a perfectly valid JSON, like you could try on the link Manish pointed.

Here are a few extra valid JSON examples, one per block:

{}

[0]

{"__comment": "json doesn't accept comments and you should not be commenting even in this way", "avoid!": "also, never add more than one key per line, like this"}

[{   "why":null} ]

{
  "not true": [0, false],
  "true": true,
  "not null": [0, 1, false, true, {
    "obj": null
  }, "a string"]
}
Community
  • 1
  • 1
cregox
  • 17,674
  • 15
  • 85
  • 116
  • 1
    Does JSON have to have double quotes? I tried to validate the OP's string above but with single quotes on jsonlint.com and it tells me it is invalid. But it's valid when using double quotes. – Ray Sep 13 '16 at 13:50
  • @Ray as usual, that doesn't have a simple yes or no answer, although I'd say it's mostly "yeah, just go with double quotes". http://json.org tells us only about using double quotes, and most places will probably follow that. However, here's a deeper investigation about it: http://stackoverflow.com/a/2275428/274502 – cregox Sep 14 '16 at 22:17
  • Improve images for SO's dark theme, please. – carloswm85 Aug 27 '20 at 11:14
  • @carloswm85 i'll leave it to you! feel free to edit the answer. (i don't own a desktop, don't know where to set the dark theme, and don't really care... ) – cregox Aug 28 '20 at 12:22
101

Your JSON object in this case is a list. JSON is almost always an object with attributes; a set of one or more key:value pairs, so you most likely see a dictionary:

{ "MyStringArray" : ["somestring1", "somestring2"] }

then you can ask for the value of "MyStringArray" and you would get back a list of two strings, "somestring1" and "somestring2".

Drilon Kurti
  • 402
  • 2
  • 18
PapaSmurf
  • 2,345
  • 1
  • 20
  • 10
  • 13
    The code example you posted is invalid, when you would try to parse that string as a json it'll throw an error/exception. The fact you say that JSON is always key/value pairs is also inherently wrong. Nothing in the JSON spec says you NEED to have key/value pairs. When talking about data transport indeed key/value pairs are the most useful structure but the string the OP posted is perfectly valid JSON: http://codebeautify.org/jsonviewer/92ac7b – ChrisR May 07 '14 at 09:28
  • 1
    I had API's on the brain, where you want to look up the value in an array based on a key. So it would be, for an un-named array, {"1":"somestring1", "2":"somestring2"} – PapaSmurf Aug 10 '14 at 23:27
46

Basically yes, JSON is just a javascript literal representation of your value so what you said is correct.

You can find a pretty clear and good explanation of JSON notation on http://json.org/

ChrisR
  • 14,370
  • 16
  • 70
  • 107
7
String strJson="{\"Employee\":
[{\"id\":\"101\",\"name\":\"Pushkar\",\"salary\":\"5000\"},
{\"id\":\"102\",\"name\":\"Rahul\",\"salary\":\"4000\"},
{\"id\":\"103\",\"name\":\"tanveer\",\"salary\":\"56678\"}]}";

This is an example of a JSON string with Employee as object, then multiple strings and values in an array as a reference to @cregox...

A bit complicated but can explain a lot in a single JSON string.

Ankur Jyoti Phukan
  • 785
  • 3
  • 12
  • 20