0

I am trying to out all the data from my json file that matches the value "data10=true" it does that but only grabs the names, how can i make it so it will output everything in my json file with anything that matches the "data10=true"?

this is what ive got data=$(jq -c 'to_entries[] | select (.value.data10 == "true")| [.key, .value.name]' data.json )

This is in my YAML template btw, running it as a pipeline in devops.

1
  • 1
    Can you provide some input and required output samples? Commented Oct 6, 2020 at 18:12

2 Answers 2

0

The detailed requirements are unclear, but hopefully you'll be able to use the following jq program as a guide:

..
| objects
| select( .data10 == "true" )
| to_entries[]
| select(.key != "data10")
| [.key, .value]

This will recursively (thanks to the initial ..) examine all the JSON objects in the input.

p.s.

If you want to make the selection based on whether .data10 is "true" or true, you could change the criterion to .data10 | . == true or . == "true".

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

4 Comments

I have a file called data.json in github and I am calling this file from my YAML template --- trigger: - none pool: name: Hosted Ubuntu 1604 steps: - script: | displayName: 'Update the build number in readme.txt' name: JQ sudo apt-get install jq echo 'installing jq' curl raw.githubusercontent.com/test/… data=$(jq .. objects | select( .data10 == true ) | to_entries[] | select(.key != "data10") | [.key, .value]' data.json) running this i am getting an error: syntax error: unexpected end of file.
You seem to be missing the first pipe symbol and the necessary single-quotes. If you're going to include the jq program on the command line, the invocation should look like: jq '.. | objects .......' data.json
Thanks peak seems it passed but i am not getting any values back..this is my data.json file:{ "FOO": { "data10":"true", "name": "Donald", "location": "Stockholm" }, "BAR": { "data10":"true", "name": "Walt", "location": "Stockholm" }, "BAZ": { "data10":"true", "name": "Jack", "location": "Whereever" } }
My mistake. I wrote true instead of "true". Changed.
0
jq 'to_entries | map(select(.value.data10=="true")) | from_entries' data.json

input data.json, with false value:

{
  "FOO": {
    "data10": "false",
    "name": "Donald",
    "location": "Stockholm"
  },
  "BAR": {
    "data10": "true",
    "name": "Walt",
    "location": "Stockholm"
  },
  "BAZ": {
    "data10": "true",
    "name": "Jack",
    "location": "Whereever"
  }
}

output:

{
  "BAR": {
    "data10": "true",
    "name": "Walt",
    "location": "Stockholm"
  },
  "BAZ": {
    "data10": "true",
    "name": "Jack",
    "location": "Whereever"
  }
}

based on: https://stackoverflow.com/a/37843822/983325

1 Comment

@rayaurelius - If it’s perfect, it would be appropriate to upvote it.

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.