json - select object without quotation using jq tool
I have the following json file:
{
"aNo": 66,
"name": "Fella"
}
{
"aNo": 77,
"name": "Bella"
}
I am trying to select the name of entry which has aNo = 66 without success. However the vice versa is working, I am using this example :
cat file.json | jq '.[] | select(.name=="Fella")
It works, I get the whole key :
{
"aNo": 66,
"name": "Fella"
}
Now what I am asking about, is :
- How to get the key based on aNo?
- How to get only the name without any other values in the result.
- Why I can't pipeline jq results ?? I get error messages when I do.
Thank you all for your great help !
to select the name of entry which has aNo = 66
This response assumes file.json contains the array:
[
{
"aNo": 66,
"name": "Fella"
},
{
"aNo": 77,
"name": "Bella"
}
]
jq approach:
jq '.[] | select(.aNo == 66).name' file.json
The output:
"Fella"
First convert the unstructured json into proper one using --slurp/-s option
--slurp/-s: Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.
cat file.json
{
"aNo": 66,
"name": "Fella"
}
{
"aNo": 77,
"name": "Bella"
}
cat file.json | jq -s
[
{
"aNo": 66,
"name": "Fella"
},
{
"aNo": 77,
"name": "Bella"
}
]
cat file.json | jq -s | jq -r '.[]|select(.aNo == 66).name'
Fella