JSON Keys

Assert if all the required keys exists in our JSON. Here we are not worried about values here, but only keys

jq operation

. | keys

Example

name: validate json keys
stages:
  - name: get product one
    request:
      url: "https://dummyjson.com/products/1"
      method: GET
    assert:
      status: 200
      body:
        - select: . | keys
          constrain: json
          want: |
            [
              "brand",
              "category",
              "description",
              "discountPercentage",
              "id",
              "images",
              "price",
              "rating",
              "stock",
              "thumbnail",
              "title"
            ]

Tip

You can also sort keys using jq operations:

. | keys | sort