Get value from JSON

How can I get a certain value out of JSON if I know one specific value. If I want to extract car type and I know the car name. I have try different variations of this: ${GetType}= Get Value From Json ${CarList.json()} $…type[?($.name=Focus)]

[
  {
    "Manuafacture": "BMW",
    "Engine": [
      {
        "volume": 3,
        "filter": 222,
        "name": "Turbo-dd"
      }
    ],
    "name": "320",
    "type": "333-44-12"
  },
  {
    "Manuafacture": "Ford",
    "Engine": [
      {
        "volume": 2,
        "filters": 1111,
        "name": "doch"
      }
    ],
    "name": "Focus",
    "type": "444-55-33"
  },
  {
    "Manuafacture": "Audi",
    "Engine": [
      {
        "volume": 3,
        "filters": 444,
        "name": "TFSI"
      }
    ],
    "name": "A6",
    "type": "999-88-77"
  }
]

I think this is one of those situations where using Python is a good idea:

import json

list_of_cars = json.load("path_to_your_file")

[car_type] = [car.get("type") for car in list_of_cars if car.get("name") == "Focus"]

This snippet can easily be turned into a keyword, if so desired.

1 Like

Hi I had the same issue (I needed an ID of a form and knew only name of the form) and I solved it using JSONLibrary. I stored the JSON response into a variable which I converted into a dictionary and then used “get value from json” as you did.

I also noticed that you dont use quotation marks for the value of a key name=‘Focus’
Exmaple of my JSON:

{
    "total": 2,
    "data": [
        {
            "id": 80,
            "name": "22.11.",
            "description": "desc",
            "isPublished": true,
            "publishedAt": "2023-11-22T11:41:08.3630569",
            "publishedVersionId": 258,
            "lastVersionId": 258,
            "publishedVersion": 2,
            "lastVersion": 2
        },
        {
            "id": 83,
            "name": "28.11.",
            "description": "desc",
            "isPublished": false,
            "publishedAt": null,
            "publishedVersionId": null,
            "lastVersionId": 265,
            "publishedVersion": null,
            "lastVersion": 1
        }
    ]
}

Here is my code:

${responseGET}=    GET  https://xxxxx.com/api/form  headers=${header}
${json}  Set Variable    ${responseGET.json()}
Convert to Dictionary    ${json}
${id}  Get value from json  ${json}  $..data[?(@.name=='${formName}')].id

Hope this helps :slight_smile:

1 Like