Skip to content

Addressing JSON

Once you have a document, then you need to be able to refer to the fields within the object. You can see from the JSON chapter that there are various 'levels' of data depth. The format of the object gives visual clues on the level of the data eg:

{  
    "documentCreatedBy":"Fred",  
    "age":55  
}  

The data is indented 1 tab - this is referred to as the 'root' of the document. The document being the overall object. To refer to the documentCreatedBy field you would simply use 'documentCreatedBy' and if interrogating the field for the value, it would return 'Fred'. So to answer the question 'who created this document?' we could look at 'documentCreatedBy' and see that it was 'Fred'

In an array, the data can be referred to by the position of the data within the square brackets eg:

{  
    "documentCreatedBy":"Fred",  
    "age":55  
    "siblings":["John","Gladys","Henry"]  
}  

We can see that Fred has siblings of "John","Gladys" and "Henry" in the Array field. An array has an 'index' which starts at 0, so the 'zeroth' entry in the array is "John", the 1st is "Gladys" and the 2nd is "Henry"; If we wanted to find the second entry in the array we would use the term 'siblings[1]' (or 'siblings.1' depending on the syntax of the context it's being used in). 'siblings[1]' would return 'Gladys'

For an object at any other level than the root the 'dot' notation is used eg:

{  
    "name": {  
        "firstName":"Fred",  
        "lastName":"Jones"
            },  
    "age":55  
    "siblings":["John","Gladys","Henry"]  
}  

'dot' notation refers to each key so if we look at the value 'Fred' we can see that Fred has a key of 'firstName' and firstName has a key of 'name' To refer to the value 'Fred' we use 'name.firstName'

For more complex structures, you simply use a combination of the above eg:

{
    "parentIdRel": [
        {
            "name": "Components Guide",
            "documentId": "e554e890-4db0-11e8-8eb3-135825763076"
        }
    ]
}

To refer to the 'Components Guide' value we would use "parentIdRel[0].name" - parentIdRel is the field - [0] is the index of object in the array - .name is the object key name