0

I am trying to get a value from the Json using JQ.

I have to get a ID from the inputJson , (activeItem) and use that ID to get the name of the element from list of items below.

Can this be done in single query ?

{
"amazon": {
"activeitem" : 2, 
"items": [
  {
    "id" : 1,
    "name": "harry potter",
    "state": "sold"
  },
  {
    "id" : 2,
    "name": "adidas shoes",
    "state": "in inventory"
  },
  {
    "id" : 3,
    "name": "watch",
    "state": "returned"
  }
]
}
}

Now i am getting the value first and the filtering, instead i want to do in single query.

1 Answer 1

1

With your data, the filter:

.amazon
| .activeitem as $id
| .items[]
| select(.id == $id)
| .name

produces:

"adidas shoes"

(Use the -r command-line option if you want the raw string.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.