Alpha
Alpha

Reputation: 23

How to get the json path based on filter on the key?

I had a json and I have to select the author name from the json object which does not contain available as a key in it

{
"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
        "available":false
    },
    {
        "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
}

So If it is possible using jsonpath or not is yes how can I achive that?

Upvotes: 2

Views: 154

Answers (3)

Yogesh Chawla
Yogesh Chawla

Reputation: 35

You can use the for..in construct to iterate through arbitrary properties of your object:

for (var key in obj.d) {
console.log("Key: " + key);
console.log("Value: " + obj.d[key]);}

Or check your available key like this, example

            var Data_Array = {
"Private": {
    "Price": {
        "Adult": "18",
        "Child": [{
            "FromAge": "0",
            "ToAge": "12",
            "Price": "10"
        }]
    }
}};var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child';console.log(child);

This will tell you whether available key is there or not, then get the author name like this, an example

By using word "b", You are still using key name.

    var info = {"fname": "A","lname": "B","Age": "34","favcolor": {"color1":"Gray", "color2":"Black", "color3":"Blue"}};

Look at the below snippet.

for(key in info) {var infoJSON = info[key];console.log(infoJSON);}

Upvotes: 0

Ninja
Ninja

Reputation: 453

I think you want the JSONpath which can do the task for you so you can try this.

$..book[?([email protected])].author

try it on https://jsonpath.com/ it works the same way you want

Upvotes: 1

hgb123
hgb123

Reputation: 14871

You could filter the book that does not have property 'available' and then map through that filtered to get the author name

const books = [
  {
    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,
    available: false
  },
  {
    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
  }
]

const authorNames = books
  .filter(book => !book.hasOwnProperty('available'))
  .map(book => book.author)

console.log(authorNames)


Reference

Object.prototype.hasOwnProperty()

Upvotes: 4

Related Questions