95

I want to convert JSON string into an array in bash. The JSON string is passed to the bash script as an argument (it doesn't exist in a file).

Is there a way of achieving it without using some temp files?

Similarly to this:

script.sh

#! /bin/bash
json_data='{"key":"value"}'
jq '.key' $json_data

jq: error: Could not open file {key:value}: No such file or directory
peak
  • 105,803
  • 17
  • 152
  • 177
Maciek Rek
  • 1,525
  • 2
  • 14
  • 18

8 Answers8

169

I would suggest using a bash here string. e.g.

jq '.key' <<< "$json_data"
jq170727
  • 13,159
  • 3
  • 46
  • 56
39

The value of the variable "json_data" that was given in the original question was not valid JSON, so this response still covers both cases (nearly-valid and valid JSON).

Valid JSON

If "$json_data" does hold a valid JSON value, then here are two alternatives not mentioned elsewhere on this page.

--argjson

For example:

 jq -n --argjson data "$json_data" '$data.key'

env

If the shell variable is not aleady an environment variable:

json_data="$json_data" jq -n 'env.json_data | fromjson.key'

Nearly-valid JSON

If indeed $json_data is invalid as JSON but valid as a jq expression, then you could adopt the tactic illustrated by the following transcript:

$ json_data='{key:"value"}'
$ jq -n "$json_data" | jq .key
"value"
Community
  • 1
  • 1
peak
  • 105,803
  • 17
  • 152
  • 177
  • I meant the 'key' to be the string type. Should probably have used the 'name' word instead. – Maciek Rek Nov 04 '17 at 08:47
  • Anyways both examples worked for me (bash on mac), even with quoted "key" and produced similar results to the here string from the jq170727's solution. – Maciek Rek Nov 04 '17 at 09:11
  • 3
    For anyone else who doesn't need the key search: it's just `jq -n $json_data`. So simple, but both the docs and online examples are super unclear for such a simple scenario. They all seem to assume you want to do something complicated. – Mike B Jan 06 '21 at 02:46
21

Use the bash: echo "$json_data" | jq '.key'

peak
  • 105,803
  • 17
  • 152
  • 177
Javier
  • 2,752
  • 15
  • 30
12

Absolutely. Just tell bash to give it a file instead.

jq '.key' <(echo "$json_data")

And make sure you run it in bash, not sh.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
8

If you want to use inline command, I found this work on my Mac:

echo '{"key":"value"}' | jq .key
Abigail Nguyen
  • 129
  • 1
  • 6
  • 1
    Store the result in a variable to use in later stages. Say your json is already in a variable and you need the result in another one. `jsonData="{"key":"value"}" result=$(echo $jsonData | jq -r '.key')` echo $result will produce value as result. – Gaurav Dalal Jul 02 '20 at 07:49
5
#! /bin/bash
json_data='{"key":"value"}'
echo $json_data | jq --raw-output '.key'
taras
  • 6,566
  • 10
  • 39
  • 50
Supun Madushanka
  • 2,738
  • 1
  • 11
  • 11
2

If you're trying to do this in a .sh file, this is what worked for me:

local json_data $(getJiraIssue "$1")               # store JSON in var
echo `jq -n "$json_data" | jq '.fields.summary'`   # pass that JSON var to jq
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
-2

Just do

$ jq '.key' <<< $'{"key":"value"}'
"value"
Logan Lee
  • 807
  • 9
  • 21
  • I donwvoted because this [doesn’t](https://unix.stackexchange.com/q/584784/48415) work in some shells due to `<<<`. – bfontaine Jan 02 '23 at 14:22