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.

peak
  • 105,803
  • 17
  • 152
  • 177

2 Answers2

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".

peak
  • 105,803
  • 17
  • 152
  • 177
  • 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 https://raw.githubusercontent.com/test/data.json?token=234234234324423edvfg 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. – rayaurelius Oct 07 '20 at 08:10
  • 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` – peak Oct 07 '20 at 08:20
  • 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" } } – rayaurelius Oct 07 '20 at 08:28
  • My mistake. I wrote `true` instead of `"true"`. Changed. – peak Oct 08 '20 at 05:01
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

Fábio Almeida
  • 189
  • 2
  • 8