1
{
"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
    }
},
"expensive": 10
}

What if I need both author and title of all the books? Like rather than having the complete data about the book if I only want some detail of it how can I get that?

Ninja
  • 433
  • 3
  • 10
  • 1
    This is not possible using JSONPath. Take a look at [this](https://stackoverflow.com/questions/40017445/selecting-a-subset-of-json-properties-and-values-using-jsonpath) answer – Morteza Jul 31 '20 at 14:36
  • @MortezaBandi I don't want key-value pair I only want these values in single JSON path? [{''author-name,book-name}] – Ninja Jul 31 '20 at 16:59
  • Ok then, @Jack Fleeting 's answer is what you want. – Morteza Aug 01 '20 at 03:27

1 Answers1

5

Depending on the implementation, this

$..book[*]['title','author']

should get you close enough.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Interesting. Do you know which parsers support this? Thanks – tom redfern Aug 03 '20 at 12:07
  • 1
    It looks like it's a matter of which implementation of jasonpath you're using: the expression works with 3 of the 4 implementations (with 2 different results, naturally...). That's why I'm not a big fan of jsonpath: https://jsonpath.herokuapp.com/ (unfortunately, there's no way to save the example so you'll have to input it manually). – Jack Fleeting Aug 03 '20 at 14:15