Checking properties of an array of objects

Checking properties of an array of objects#

Given the sample data set below, we want to check if the price of each book is greater than 10.

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95,
        "tags": ["book", "Rees", "reference", "Sayings"]
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99,
        "tags": ["book", "Waugh", "fiction", "Sword"]
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99,
        "tags": ["book", "Melville", "fiction", "Moby"]
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99,
        "tags": []
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

Steps:

  1. Define a ref that returns an array of the objects you are interested in. In this case we want to check the price of each book, so we can use $.store.book[*] as ref.

    Note

    You can use the jsonPath online evaluator to check your jsonPath syntax.

  2. Define a condition that checks the price of each book. In this case we can use all(ref, "$.price > 10") as condition.

    Note

    There are also other operators like any, none and one that you can use.

Your config file then looks like this:

checks:
  - name: book_prices_greater_than_10
    ref: $.store.book[*]
    condition: all(ref, "$.price > 10")

For the example data set above, the result of this check would be: RED.