简体   繁体   中英

When bash jq parsing json ,return a multi-line array but, how can I use this result?

when I use the command:

ids=`cat $myfilename | jq -r "[.data[].id]"`

and return like this:

[
  "PC_PACX_0017",
  "GS_ZGRS_0001",
  "PC_PACX_0018",
  "GS_ZGRS_0002",
  "AB_HXJK_0002",
  "AB_HXJK_0001",
  "TK_TKZX_0011",
  "TL_TBAL_0002",
  "TL_TBAL_0001",
  "TS_TKRS_0001",
  "TS_TKRS_0002",
  "TS_TKRS_0003"
]

but it can't be seen as a array, it's length only be 1.How can I use this result?

An array is distinct from a multi-line string. Assuming there are no newlines in your data, changing your jq call to

jq -r '[.data[].id]|.[]'  # Could be simplified to '.data[].id'

and use readArray ( bash 4 or later) to populate a proper array.

readArray -t ids < <(jq -r '[.data[].id]|.[]' "$myfilename")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM