Search code examples
jsonreact-nativechildren

Accessing Children in Json Array of Objects


Created a Mock Json server and able to access it via localhost:3000/JOBS. How do I access the children "Parcels" in URL like accessing the type of open like localhost:3000/JOBS?type=open. Thanks in advance.

I have a Json Array of objects as below:

{
  "JOBS": [
    {
      "Id": "1",
      "type": "open",
      "driver": "Andy",
      "receiver": "Paul",
      "price": "635.50",

      "Parcels": [
        {
          "parcelId": "90"
        }
      ]
    }
  ]
}

Solution

  • Right now you cant access only Parcels as is.

    You need to modify your tree structure, in order to access Parcels as

    {
        "JOBS": [
            {
                "Id": "1",
                "type": "open",
                "driver": "Andy",
                "receiver": "Paul",
                "price": "635.50"
            }
        ],
        "PARCELS": [
            {
                "parcelId": "90"
            }
        ]
    }
    

    and access it as localhost:3000/PARCELS as mentioned in this link and described here