Loop through JSON object using jq

半城伤御伤魂 提交于 2021-02-07 23:23:13

问题


This is what my JSON array of objects looks like:

[
    {
        "Description": "Description 1",
        "OutputKey": "OutputKey 1",
        "OutputValue": "OutputValue 1"
    },
    {
        "Description": "Description 2",
        "OutputKey": "OutputKey 2",
        "OutputValue": "OutputValue 2"
    },
    {
        "Description": "Description 3",
        "OutputKey": "OutputKey 3",
        "OutputValue": "OutputValue 3"
    },
    {
        "Description": "Description 4",
        "OutputKey": "OutputKey 4",
        "OutputValue": "OutputValue 4"
    },
    {
        "Description": "Description 5",
        "OutputKey": "OutputKey 5",
        "OutputValue": "OutputValue 5"
    },
    {
        "Description": "Description 6",
        "OutputKey": "OutputKey 6",
        "OutputValue": "OutputValue 6"
    }
]

How do i iterate over this using jq so that i can use the values of OutputKey and OutputValue in other commands?


回答1:


Assuming your content is coming from in.json:

#!/usr/bin/env bash
case $BASH_VERSION in (""|[123].*) echo "Bash 4.0 or newer required" >&2; exit 1;; esac

declare -A values=( ) descriptions=( )

while IFS= read -r description &&
      IFS= read -r key &&
      IFS= read -r value; do
  values[$key]=$value
  descriptions[$key]=$description
  echo "Read key $key, with value $value and description $description" >&2
done < <(jq -r '.[] | (.Description, .OutputKey, .OutputValue)' <in.json)

Given your input, this emits the following to stderr:

Read key OutputKey 1, with value OutputValue 1 and description Description 1
Read key OutputKey 2, with value OutputValue 2 and description Description 2
Read key OutputKey 3, with value OutputValue 3 and description Description 3
Read key OutputKey 4, with value OutputValue 4 and description Description 4
Read key OutputKey 5, with value OutputValue 5 and description Description 5
Read key OutputKey 6, with value OutputValue 6 and description Description 6

Moreover, after this code is run, you can then execute:

key_to_look_up="OutputKey 1"
echo "${values[$key_to_look_up]}"
echo "${descriptions[$key_to_look_up]}"

...and get as output:

OutputValue 1
Description 1



回答2:


  1. I'm afraid your question isn't very clear. If you want to produce the values for consumption by some tool or application other than jq, then it would be helpful to know what that tool expects. If you want to use the values in jq itself, then you could use map or reduce; alternatively, you could use a filter having the form: .[] | ... or [.[] ...], where the ... is some jq code that accesses the values of interest, e.g.

    [.[] | [.OutputKey, .OutputValue] ]
    
  2. If you want to perform some reduction operation, then you will probably want to use the form: reduce .[] as $x (_; _)

  3. There are other alternatives, depending on what it is you are trying to do.



来源:https://stackoverflow.com/questions/39736840/loop-through-json-object-using-jq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!