PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] how to jq by the desired key is inside nested json

 July 23, 2022     bash, jq, json, shell     No comments   

Issue

Here is the id.json

{
    "name": "peter",
    "path": "desktop/name",
    "description": "male",
    "env1": {
        "school": "AAA",
        "height": "150",
        "weight": "80"
    },
    "env2": {
        "school": "BBB",
        "height": "160",
        "weight": "70"
    }
}

it can be more env3, env4, etc created automatically I am trying to get the env1 by using height and weight as key so the output can look like:

env1:height:150
env1:weight:80
env2:height:160
env2:weight:70
env3:height:xxx
.
.
.

My shell command jq .env1.height... id.json tried can only get the output by using env1, env2 as key, but it cannot handle env3, env4. And also, using jq to_entries[] to convert the json defined by key and value, but the first few row made me cannot get .value.weight as output. Any idea please?

Update: edited the json to remove these three line

"name": "peter",
"path": "desktop/name",
"description": "male",

Then run below command:

jq 'to_entries[] | select(.value.height!=null) | [.key, .value.height, .value.weight]' id2.json 

I can get below result

[
  "dev",
  "1",
  "1"
]
[
  "sit",
  "1",
  "1"
]

This is almost what I need, but any idea to remove the outer level json please?


Solution

Using your data as initially presented, the following jq program:

keys_unsorted[] as $k 
| select($k|startswith("env"))
| .[$k] | to_entries[]
| select(.key|IN("height","weight"))
| [$k, .key, .value]
| join(":")

produces

env1:height:150
env1:weight:80
env2:height:160
env2:weight:70

An answer to the supplementary question

According to one interpretation of the supplementary question, a solution would be:

keys_unsorted[] as $k 
| .[$k]
| objects
| select(.height and .weight)
| to_entries[]
| select(.key|IN("height","weight"))
| [$k, .key, .value]
| join(":")

Equivalently, but without the redundancy:

["height","weight"] as $hw
| keys_unsorted[] as $k 
| .[$k]
| objects 
| . as $object
| select(all($hw[]; $object[.]))
| $hw[]
| [$k, ., $object[.]]
| join(":")


Answered By - peak
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing