Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you represent a JSON array of strings?

Tags:

json

This is all you need for valid JSON, right?

["somestring1", "somestring2"]
like image 399
finneycanhelp Avatar asked Oct 14 '22 07:10

finneycanhelp


People also ask

Can JSON have array of strings?

JSON is JavaScript Object Notation is used for data interchange, Array of strings is an ordered list of values with string type. So on a whole, the 'JSON array of strings' represents an ordered list of values, and It can store multiple values. This is useful to store string, Boolean, number, or an object.

What is the correct way to write a JSON array?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.

How a string is represented in JSON?

A string is a sequence of zero or more Unicode characters, enclosed between " and " (double quotes). Strings wrapped in single quotes ' are not valid. JSON Strings can contain the following backslash-escaped characters: \" – Double quote.


1 Answers

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"]
}
like image 110
cregox Avatar answered Nov 14 '22 03:11

cregox