Check if any value is repeated in jsonPath

I have jsonPath as below

{   "book":
        [ 
            { "category": "reference",
              "author": "Nigel Rees",
              "title": "Sayings of the Century",
              "price": 8.95
            },
            { "category": "fiction",
              "author": "Nigel Rees",
              "title": "Sword of Honour",
              "price": 12.99
            }
]}

And want to check if any author name is repeated? I tried

$.book[?(@.author=='Nigel Rees')].find(1)

But it always throws an exception that didn’t find anything, how can I verify that author='Nigel Rees'ie entries author='Nigel Rees'have two books?

+2
source share
1 answer

Depends on what you plan to do if author names exist. If you only need objects with an author Nigel Reese, you can use filter .

    var booksByNigelReese = book.filter( function(book, index) {
        return book.author === 'Nigel Reese'
    })

.filter() , , chooes, , true false

+1

Source: https://habr.com/ru/post/1665611/


All Articles