Mona Coder
Mona Coder

Reputation: 6316

How to Pass an Array to JSON Properly

I am trying to pass an array of objects like "make":"Ford", "trim":"escape", "year":2014 into a JSON object car at following exaample

var player = {
    "name":"John",
    "age":30,
    "car": [{"make":"Ford", "trim":"escape", "year":2014 }]
}

but I can not understand why I have to wrap the elemet in{ }. I mean why the

"car": ["make":"Ford", "trim":"escape", "year":2014 ]

array without {} is not working. I am not willing to create array of array here so why do I need to wrap theim in {}

Upvotes: 2

Views: 545

Answers (5)

Nick Taras
Nick Taras

Reputation: 746

The {} represent and object and the [] represent an array.

You could change your data to the example below if a player can only have one car.

player = {
    "name":"John",
    "age":30,
    "car": {"make":"Ford", "trim":"escape", "year":2014 }
}

However, if the player could have many cars then you should place the object inside an array, as per your code example.

[{"make":"Ford", "trim":"escape", "year":2014 }]

Arrays can only store one data type per entry e.g. array['string', 'number', 'object'].

However, you will find that arrays are typically built up of one data type e.g. array['string', 'string', 'string'].

This is where a combination of an array (to store a list of items) and an object (to encapsulate data of a list item) work really well together.

An example of what you could do with the player variable you have designed:

player = {
    "name":"John",
    "age":30,
    "cars": [
            {"make":"Ford", "trim":"escape", "year":2014 },
            {"make":"Audi", "trim":"escape", "year":2017 },
            {"make":"Seat", "trim":"escape", "year":2018 }
           ]
}

Then we can access each object using syntax like so:

const firstCar = player.cars[0];
const audi = player.cars[1];
const lastCar = player.cars[player.cars.length - 1];

Hope this helps you with your web app / website :)

Upvotes: 1

Hero Wanders
Hero Wanders

Reputation: 3292

May it be you are coming from PHP where you have associative arrays? If you want to have something similar in JavaScript, just use objects! In case the value of car should directly contain the properties, you should use this notation:

"car": {"make":"Ford", "trim":"escape", "year":2014 }

In JavaScript objects and arrays are different species: arrays are ordered lists which are only indexed by numbers (its indices which range from 0 to length - 1) and objects are unordered structures which have named properties (like "make", "trim" and "year"). Objects support property listing/enumeration too but that's another story.

There are some basic types in JSON which every value belongs to. Basically there are strings, numbers, booleans, arrays, objects or null, not more. (JavaScript has additions like undefined or symbols etc.).

Each array entry and each object property, if written down in JavaScript object notation (JSON), like you are trying to do, has to be of one of these types. In particular an array is written as a comma-separated list of values. Clearly your example "make":"Ford" is not a single value but a notation for a property-value-assignment in an object.

I have found a little tutorial on JSON: https://www.digitalocean.com/community/tutorials/an-introduction-to-json

Upvotes: 1

iPirat
iPirat

Reputation: 2206

Braces {} denote an object. So basically, {"make":"Ford", "trim":"escape", "year":2014 } would describe a single car.

if John can only have a single car, you can use something like that:

var player = {
    "name":"John",
    "age":30,
    "car": {"make":"Ford", "trim":"escape", "year":2014 }
}

if John can have multiple cars, then you should use an array like this:

var player = {
    "name":"John",
    "age":30,
    "cars": [{"make":"Ford", "trim":"escape", "year":2014 }, {"make":"Audi", "trim":"RS8", "year":2018 } /* more cars */ ]
}

Upvotes: 3

Peteris
Peteris

Reputation: 3325

Arrays vs Objects

In JSON, arrays consist of values. Values can be strings ("year"), numbers (2014), objects ( something in {}) or other arrays (something in []). "make":"Ford" is not one of these things, it's not a basic value, so it can't be part of an array.

The other thing that JSON allows is objects (like {"make":"Ford", "trim":"escape", "year":2014}) - those consist of string:value pairs like "make:"Ford".

An object is not an array; an array is not an object. The JSON specification (https://www.json.org/) might be helpful.

If you literally want to pass an array of objects, as you state in the first sentence, then you need to have multiple separate objects - not ["make":"Ford", "trim":"escape", "year":2014], but [{"make":"Ford"}, {"trim":"escape"}, {"year":2014}] - that would be an array of three separate, unrelated objects.

Upvotes: 1

Samuel Roberto
Samuel Roberto

Reputation: 461

You code cannot work because you’re trying to create an array of type Object and in Javascript an Object is wrapped into {} tags

Upvotes: 1

Related Questions