jq子对象中所有对象的打印键和值


13

我发现此Q / A解决方案可以打印对象中的所有键:

jq -r 'keys[] as $k | "\($k), \(.[$k] | .ip)"' 

就我而言,我想对子对象执行以上操作:

jq -r '.connections keys[] as $k | "\($k), \(.[$k] | .ip)"'

正确的语法是什么?

Answers:


21

只需通过管道即可keys运行:

样品input.json

{
    "connections": {
        "host1": { "ip": "10.1.2.3" },
        "host2": { "ip": "10.1.2.2" },
        "host3": { "ip": "10.1.18.1" }
    }
}

jq -r '.connections | keys[] as $k | "\($k), \(.[$k] | .ip)"' input.json

输出:

host1, 10.1.2.3
host2, 10.1.2.2
host3, 10.1.18.1

7
keys对键进行排序,因此值得指出的keys_unsorted是不然。
达到峰值

1
@ peak,OP写道“ 我找到了这个stackoverflow.com/questions/34226370/… ... ”,其中公认的答案清楚地表明“ keys按排序顺序生成键名;如果要按原始顺序键名,请使用keys_unsorted ”。因此,OP意识到了这一点,并进行了有keys意识的选择。
RomanPerekhrest

3
该评论是针对其他遇到此问答的人的。
达到峰值

0

一个更通用的bash函数来导出vars(带插值):

#
#------------------------------------------------------------------------------
# usage example:
# doExportJsonSectionVars cnf/env/dev.env.json '.env.virtual.docker.spark_base'
#------------------------------------------------------------------------------
doExportJsonSectionVars(){

   json_file="$1"
   shift 1;
   test -f "$json_file" || echo "the json_file: $json_file does not exist !!! Nothing to do" && exit 1

   section="$1"
   test -z "$section" && echo "the section in doExportJsonSectionVars is empty !!! nothing to do !!!" && exit 1
   shift 1;

   while read -r l ; do
      eval $l ;
   done < <(cat "$json_file"| jq -r "$section"'|keys_unsorted[] as $key|"export \($key)=\(.[$key])"')
}

示例数据

cat cnf/env/dev.env.json
{
  "env": {
    "ENV_TYPE": "dev",
      "physical": {
        "var_name": "var_value"
      },
      "virtual": {
          "docker": {
            "spark_base": {
                "SPARK_HOME": "/opt/spark"
              , "SPARK_CONF": "$SPARK_HOME/conf"
            }
            , "spark_master": {
              "var_name": "var_value"
            }
            , "spark_worker": {
              "var_name": "var_value"
            }
          }
          , "var_name": "var_value"
      }
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.