Dynamic Keys

JSON data is written as name/value pairs, just like JavaScript object properties.
Lets assume that we want to create a Person object that contains a First Name and a Last Name.
There are actually two different ways an object can be represented in JSON.


Format 1 - Array Format

This is the normal way:

"staff" : 
[
  {
    "id": 1,
    "firstName":"John",
    "lastName":"Doe"
  },
  {
    "id": 2,
    "firstName":"Anna",
    "lastName":"Smith"
  }
]

Format 2 - Dynamic Keys

However there is another format that can be used and this format uses a Dynamic Key.
In this format the "id" is used as the key.
This relies on the <key> to access a specific staff object.

"staff" : 
{
   "1" :
   {
      "firstName":"John",
      "lastName":"Doe"
   },
   "2" :
   {
      "firstName":"Anna",
      "lastName":"Smith"
   }
}



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext