Convert a nested JSON of objects into array into a

2020-04-24 16:47发布

I am doing something fundamentally wrong but just can't see what, could some kind person point out my fault with jq or JSON here?

I have the the following child objects contained within an array “entries

{
    "profile": {
        "name": "TesterRun1",
        "download": {
            "entries": [{
                    "ENTRY_A": "testserver1_place_com",
                    "store": "A",
                    "type": "direct"
                },
                {
                    "ENTRY_B": "testserver2_anotherplace_com",
                    "store": "B",
                    "type": "bypass"
                },
                {
                    "ENTRY_B": "testserver2_anotherplace_com",
                    "store": "A",
                    "type": "bypass"
                }
            ]
        }
    }
}

I wish to convert these to an array accessible by bash via the jq function “to_entries” using the below query but so far nothing!

jq 'to_entries|.[]|.profile.download.entries|select(.store=="A")|.[]'

You can see here that nothing is returned on JQ Play - enter link description here

Please help save my sanity, what am I doing wrong

2条回答
一纸荒年 Trace。
2楼-- · 2020-04-24 17:31

Here is a slightly different approach (with some cleaned-up data) which captures the output from jq into separate column arrays.

#!/bin/bash
data='{
  "profile": {
    "name": "TesterRun1",
    "download": {
      "entries": [
        {
          "entry": "testserver1_place_com",
          "store": "A",
          "type": "direct"
        },
        {
          "entry": "testserver2_anotherplace_com",
          "store": "B",
          "type": "bypass"
        },
        {
          "entry": "testserver2_anotherplace_com",
          "store": "A",
          "type": "bypass"
        }
      ]
    }
  }
}'
filter='
        .profile.download.entries[]
      | select(.store == "A")
      | .entry, .store, .type
'      
declare -a ENTRY
declare -a STORE
declare -a TYPE
i=0
while read -r entry; read -r store; read -r type; do
    ENTRY[$i]="$entry"
    STORE[$i]="$store"
    TYPE[$i]="$type"
    i=$((i + 1))
done < <(jq -Mr "$filter" <<< "$data")
declare -p ENTRY STORE TYPE

Output

declare -a ENTRY='([0]="testserver1_place_com" [1]="testserver2_anotherplace_com")'
declare -a STORE='([0]="A" [1]="A")'
declare -a TYPE='([0]="direct" [1]="bypass")'
查看更多
在下西门庆
3楼-- · 2020-04-24 17:38

to_entries has nothing whatsoever to do with exposing JQ results to bash. Rather, it takes each entry in a JSON object and emits a {"key": key, "value": value} pair.

That can be useful, if you want to identify and extract arbitrary keys. For example:

#!/usr/bin/env bash

jq_script='
.profile.download.entries[]
| select(.store == "A")
| to_entries[]
| select(.key != "store")
| select(.key != "type")
| [.key, .value]
| @tsv
'

declare -A array=( )
while IFS=$'\t' read -r key value; do
  array[$key]=$value
done < <(jq -r "$jq_script")

# print array output
declare -p array

...will, when given your input on stdin, emit (albeit on a single line, without the whitespace changes):

declare -A array=([ENTRY_A]="testserver1_place_com"
                  [ENTRY_B]="testserver2_anotherplace_com" )

...which I assume, for lack of any better description in the question, is what you actually want.

查看更多
登录 后发表回答