0

At the moment I have multiple arrays inside an object containing different pieces of data.

The issue is that the JSON is invalid and I am not sure who to correct it.

Here is my current code:

{
    "cars": {
        [{
            "model": 'test'
        }],
        [{
            "model": 'test2'
        }]
    }
};

Any help would be appreciated!

3
  • 5
    I think you want just one array, with multiple objects inside it: {"cars": [{"model":"test"},{"model":"test2"}]} is valid JSON. Commented Oct 29, 2018 at 1:57
  • 1
    And if I'm not mistaking, only double quotes are valid json Commented Oct 29, 2018 at 1:58
  • JSON is based on key:value pairs. Your second array is not a part of a pair. Commented Oct 29, 2018 at 1:59

1 Answer 1

2

Use key:value pairs and remove that semicolon.

{
    "cars": [
        {
            "model": "test"
        },
        {
            "model": "test2"
        }
    ]
}

Then once you parse your JSON and assign it to a variable, e.g. jsonVar, you can loop over the array jsonVar.cars to get each dictionary, which has model property.

More examples of correctly formatted JSON.

Finally, this JSON validator can provide helpful hints on incorrectly formatted JSON.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.