I tried adding an array with dictionaries into a JSON file, but it returns a bunch of errors, so I’m wondering if is it possible to add dictionaries into a JSON file.
(I’m not trying to push data into the JSON file using Node.JS, I’m trying to manually put dictionaries into the JSON file)
landmarks.json:
{
"Isle of Dreams": {
"name": "Isle of Dreams",
"description": "An island surrounded by mist and myth, here dreamers come to fulfill their deepest desires."
},
"the Temple of Shadows": {
"name": "the Temple of Shadows",
"description": "",
"droptable": [
{ id: 0, weight: 0.2, item: "Lemon" },
{ id: 1, weight: 0.3, item: ['Grape', 'Orange', 'Apple'] },
{ id: 2, weight: 0.4, item: 'Mango' },
{ id: 3, weight: 0.1, item: 3 }
]
}
}
2
Answers
The JSON file needs
"
symbols around the keys:For example,
should be
That is what the error messages are trying to tell you.
It needs double-quotes, not single-quotes, around strings
Not
but
Explanation
Inside Javascript itself, we are allowed to use the shorthand notation of dropping the
"
around keys, but we must remember that:"
back in, and they have to be double-quotes, not single quotes'
, andYou should change your JSON like this: