Can a JSON array contain objects of different key/value pairs?

34,784

Solution 1

You can use any structure you like. JSON is not schema based in the way XML is often used and Javascript is not statically typed.

you can convert your JSON to a JS object using JSON.parse and then just test the existence of the property

var obj = JSON.parse(jsonString);
if(typeof obj.example[0].firstName != "undefined") {
   //do something
}

Solution 2

It doesn't matter you can mix and match as much as you want.

You could just test it

typeof someItem.example !== 'undefined' // True if `example` is defined.

Solution 3

It's perfectly valid JSON. Personally I prefer this syntax better, because it reads easier:

{
    "example": [
        {
            "firstName": "John",
            "lastName": "Doe"
        },
        {
            "fruit": "apple"
        },
        {
            "length": 100,
            "width": 60,
            "height": 30
        }
    ]
}

As to answer your second question, you can read data from a JSON string by using var data = JSON.parse(datastring);. Then simply use it by calling data.property.secondlevel. Any variable can be an object, array, string or number, allowing for nested structures.

Solution 4

You are free to do what you want with the contents of the array. Jus remember about this before you try to iterate an access properties of each item in your array.

one thing: you won't be able to deserialize this to anything else than an array of object in your server side, so don't have surprises later.

as a hint, maybe you could include a common field in the objects specifying the "type" so later is easy to process.

var array = [{"type":"fruit", "color":"red"},
{"type":"dog", "name":"Harry"}];

var parser = {
    fruit:function(f){
   console.log("fruit:" + f.color);
    },
    dog: function(d){
    console.log("dog:"+d.name);
    }};

for(var i=0;i<array.length;i++){
    parser[array[i].type](array[i]);
}
Share:
34,784
tonga
Author by

tonga

Updated on December 24, 2020

Comments

  • tonga
    tonga over 3 years

    Can a JSON array contain Objects of different key/value pairs. From this tutorial, the example given for JSON array consists of Objects of the same key/value pair:

    {
      "example": [
        {
          "firstName": "John",
          "lastName": "Doe"
        },
        {
          "firstName": "Anna",
          "lastName": "Smith"
        },
        {
          "firstName": "Peter",
          "lastName": "Jones"
        }
      ]
    }
    

    If I want to change it to have different key/value pairs inside the JSON array, is the following still a valid JSON?

    {
      "example": [
        {
          "firstName": "John",
          "lastName": "Doe"
        },
        {
          "fruit": "apple"
        },
        {
          "length": 100,
          "width": 60,
          "height": 30
        }
      ]
    }
    

    Just want to confirm this. If so, how can I use JavaScript to know if the JSON "example" field contains the first homogeneous objects or the second heterogeneous objects?