The file format of a JSON file is shown at its home page: https://www.json.org/json-en.html , the ‘weird’ line diagrams with black lines and white boxes. It's a railroad-diagram ( https://en.wikipedia.org/wiki/Syntax_diagram ), since you read the diagram like a train follows its railroad (no sharp turns).
The JSON format is very simple.
A number is written like a number, eg “123”, or “1.31e-56” . The “number” diagram at the JSON page should allow my examples. A piece of text is written as a string “hello world”. You also have the boolean values “true” and “false”, and “null” for ‘no value’.
You can have a sequence of elements (it's named ‘a list’) by writing “[” and “]” around it and seperating the elements by “,”, eg “[1,2,3,4]” are 4 numbers in a list. Lists in lists etc is also allowed, ie “[1, [4,56], [ ] ]” where the “[ ]” is an empty list, a list without any element.
You can have a dictionary (also known as map) of named values. JSON names it “object”. It looks like
{ "length" : 3,
"description" : "short",
"value": [1, 2, 3]
}
It's much like a list, except its elements are of the form ‘ “text” : value ’ where ‘value’ can be any value allowed in JSON, including another dictionary.
So for your example in the first post, you could do something like
# I use lines starting with a '#' as comment. JSON doesn't allow comment, so you
# have to delete those lines if you want to use the example in your code.
# GENERAL IDEA:
# JSON insists you start with an 'object', so the file must have a disctionary
# as its outermost value.
# I used an object with an element named "strings" that has the list of strings
# that you want.
{ "strings" : [
....
]
}
# FIRST ATTEMPT
# The simplest is to use a list of lists of strings, where a list with 1 string
# is a line string, and a list with multiple values is a block string:
{ "strings" : [
[ "line string" ],
[ "another line string" ],
[ "block string",
"more text",
"and another one" ],
]
}
# SECOND ATTEMPT
# A problem with the above is that your data values only have an index number, ie
# 'object["strings"][1]' is '["another line string"]'.
# You may want to attach a name to your data instead.
{ "strings" : [
{ "name": "first-string",
"value": [ "line string" ] },
{ "name": "another-string",
"value": [ "another line string" ] },
{ "name": "first-block",
"value": [ "block string",
"more text",
"and another one" ] }
]
}
# 'object["strings"][1]' is now another dictionary, where you can access a
# 'name' and a 'value' field to know what data value you have. In this way
# It is simpler to get the right string value at the right spot in your program.
As you can see, I deliberately formatted the JSON data to make it more readable. The JSON file format allows any amount of white space, including none at all, so
{"strings":[{"name":"first-string","value":["line string"]},{"name":
"another-string","value":["another line string"]},{"name":"first-block","value":[
"block string","more text","and another one"]}]}
is also allowed. It's however much less easy to understand what it specifies. This is what I meant with ‘treat the contents of such files like you treat your program code’. Make your data file as readable as you can, it avoids errors and saves time in understanding what you wrote.