skip to Main Content

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 }
  ]
 }
}

Error:
description?

2

Answers


  1. The JSON file needs " symbols around the keys:

    For example,

    { id: 0, weight: 0.2, item: "Lemon" },
    

    should be

    { "id": 0, "weight": 0.2, "item": "Lemon" },
    

    That is what the error messages are trying to tell you.

    It needs double-quotes, not single-quotes, around strings

    Not

     { id: 1, weight: 0.3, item: ['Grape', 'Orange', 'Apple'] 
    

    but

     { "id": 1, "weight": 0.3, "item": ["Grape", "Orange", "Apple"] 
    

    Explanation

    Inside Javascript itself, we are allowed to use the shorthand notation of dropping the " around keys, but we must remember that:

    • when we want to write JSON, we need to put those " back in, and they have to be double-quotes, not single quotes ', and
    • if we want to put in a variable for the key, we need to put square brackets around the key
    Login or Signup to reply.
  2. You should change your JSON like this:

    {
        "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
                }
            ]
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search