1

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 :

  1. How to get the key based on aNo?
  2. How to get only the name without any other values in the result.
  3. Why I can't pipeline jq results ?? I get error messages when I do.

Thank you all for your great help !

3
  • 1
    With jq you don't need to use cat at all. "jq .aNo" will give you the aNo value and "jq .name" will give you the name. Commented May 4, 2017 at 13:40
  • 1
    "How to get the key based on aNo?"... what key? "How to get only the name without any other values in the result."... actually select the fields that you want. "Why I can't pipeline jq results ?? I get error messages when I do."... what error? If you got errors, put them in your question along with the exact data and code you used... Commented May 5, 2017 at 0:15
  • @fanarweb - Please clarify your questions using standard JSON terminology. E.g., does the file contain a JSON array, or a stream of JSON objects? Please also use the word "key" with care. Commented May 6, 2017 at 9:38

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

4 Comments

The data as-is is valid as far as jq in concerned. It's treated as a stream of inputs, rather than a single input. Though with that said, assuming that is the input, his selector would not yield those results. It probably is an array of objects.
@JeffMercado, what you are saying to me is unclear. The OP had written concrete phrase: I have the following json file:. I didn't expect any stream of inputs as you said
All I meant was by stating what "valid json should look like", you're implying (to me) that the input as he has it is invalid. Assuming he posted the contents of the file as-is, it's valid to jq. Only it's not an array of objects, it's one object, followed by another.
@JeffMercado, I think he meant an array of objects. Otherwise, the question's description should be elaborated
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.