Forum Discussion

raygoel's avatar
raygoel
Occasional Contributor
2 years ago

Unable to get all elements from JSON Response using JSON Path

Hi All, 

 

I have a JSON Response like this - 

[
{
"Id" : "b0f9009b-87bd-4b68-9033-bb6027f1251e",
"Name" : "Carousel",
"Mode" : "Simulation",
"State" : "Offline",
"OnlineState" : "NotOnline",
"Active" : false,
"Available" : true,
"DockState" : "Docked",
"Present" : true,
"IsSimulated" : true,
"IsInstrument" : true,
"Priority" : 2,
"FirmwareVersion" : "Not available.",
"SoftwareVersion" : "Not available.",
"ModelDescription" : "Not available.",
"SerialNumber" : "Not available.",
"ConnectionMode" : "Unknown",
},
{
"Id" : "c2fc48ef-8190-495b-86ee-4b16fb793fa3",
"Name" : "Hotel",
"Mode" : "Normal",
"State" : "Offline",
"OnlineState" : "NotOnline",
"Active" : false,
"Available" : true,
"DockState" : "Docked",
"Present" : true,
"IsSimulated" : false,
"IsInstrument" : true,
"Priority" : 2,
"FirmwareVersion" : "Not available.",
"SoftwareVersion" : "Not available.",
"ModelDescription" : "Not available.",
"SerialNumber" : "Not available.",
"ConnectionMode" : "Unknown",
},
{
"Id" : "2e2b98cc-96ec-4e81-933a-aa7c4e762220",
"Name" : "MultidropCombi",
"Mode" : "Simulation",
"State" : "Offline",
"OnlineState" : "NotOnline",
"Active" : false,
"Available" : true,
"DockState" : "Docked",
"Present" : true,
"IsSimulated" : true,
"IsInstrument" : true,
"Priority" : 2,
"FirmwareVersion" : "Not available.",
"SoftwareVersion" : "Not available.",
"ModelDescription" : "Not available.",
"SerialNumber" : "Not available.",
"ConnectionMode" : "Unknown",
},
{
"Id" : "66703155-9db2-46cd-91f2-21d4a7b76a43",
"Name" : "Spinnaker",
"Mode" : "Simulation",
"State" : "Offline",
"OnlineState" : "NotOnline",
"Active" : false,
"Available" : true,
"DockState" : "Docked",
"Present" : true,
"IsSimulated" : true,
"IsInstrument" : true,
"Priority" : 1,
"FirmwareVersion" : "Not available.",
"SerialNumber" : "Not available.",
"ConnectionMode" : "Unknown",
},
{
"Id" : "516fbe5b-56c0-4ff2-a19d-3a9da703bc43",
"Name" : "Teleshake",
"Mode" : "Simulation",
"State" : "Offline",
"OnlineState" : "NotOnline",
"Active" : false,
"Available" : true,
"DockState" : "Docked",
"Present" : true,
"IsSimulated" : true,
"IsInstrument" : true,
"Priority" : 2,
"FirmwareVersion" : "Not available.",
"SoftwareVersion" : "Not available.",
"ModelDescription" : "Not available.",
"SerialNumber" : "Not available.",
"ConnectionMode" : "Unknown",
}
]

 I am trying to get the ID of each device from this response in the Data Source step using JSON path. When I enter $[*]['Id'] I get 0 rows. However, if I enter something like $[1]['Id'] then I get the corresponding ID correctly. 


I would like to retrieve the ID, one at a time and feed it to another endpoint using data source loop to verify the details by ID. Can someone please help me with this? 

Here is a screenshot of my Data Source.

Thanks,

Ray

3 Replies

  • richie's avatar
    richie
    Community Hero
    Hey raygoel,

    There's a number of ways to do this, but the answer kinda depends on whether your response is static inasmuch that the json you've published has 5 objects in the array. Are you always gonna get 5 objects (or less) or will the number of objects change?
    Reason i'm asking is that if the response is dynamic youll need groovy script, if it's static, we can do it with the embedded functionality no problem.

    Cheers,

    Rich
    • raygoel's avatar
      raygoel
      Occasional Contributor

      Thanks for your response richie 

       

      The response depends on how the application is setup. In this case, I have set it up so that this response is always going to be static. I tried using the wild card notation for JSON path $[*]['Id'] but it did not work in this case. However, if I try it online with a JSON path explorer it correctly returns the IDs. 

       

      -Ray

    • raygoel's avatar
      raygoel
      Occasional Contributor

      Any more ideas about solving this problem? For now, I have built a grid of data to assert against since the response and data will be static.