1
votes

With the following json object.

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

Using JSONPath. Is there any expression that can get only the key-value pair "author" and "title" for all books costing less that 13.00. The result will be something like:

[{
    "author": "Nigel Rees",
    "title": "Sayings of the Century"
  }, {
    "author": "Evelyn Waugh",
    "title": "Sword of Honour"
  }, {
    "author": "Herman Melville",
    "title": "Moby Dick"
  }
]

I started with $.store.book[?(@.price < 13)] but this gets all the elements on each book costing less than 13.00. Not the desired behavior. Any help is much appreciated.

1

1 Answers

1
votes

You are close; this expression:

$.store.book[?(@.price < 13)]["author","title"]

or, depending on the implementation

$.store.book[?(@.price < 13)][]author,title

should get you

[
   {
      "author" : "Nigel Rees",
      "title" : "Sayings of the Century"
   },
   {
      "author" : "Evelyn Waugh",
      "title" : "Sword of Honour"
   },
   {
      "author" : "Herman Melville",
      "title" : "Moby Dick"
   }
]

or

[
   "Nigel Rees",
   "Sayings of the Century",
   "Evelyn Waugh",
   "Sword of Honour",
   "Herman Melville",
   "Moby Dick"
]