Matt Rek
Matt Rek

Reputation: 1801

Can I pass a string variable to jq rather than passing a file?

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

Upvotes: 117

Views: 124378

Answers (9)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4739

Yes indeed it can be done.

Below snippet is part of the script which attempts to communicate to browserstack api to get the build status.

Sample response is like below

[
    {
        "automation_build": {
            "name": "Build #183: sample-app.apk v1.0",
            "hashed_id": "13652db4394d39e59e2defafaa153e9da14827d",
            "duration": 1736,
            "status": "failed",
            "build_tag": null,
            "public_url": null
        }
    }
]

And below script parses this response and gets the status of the build

BS_CREDENTIALS="${BS_CREDENTIALS:-}"
BS_BUILD_STATUS_API='https://api.browserstack.com/app-automate/builds?limit=1'

#Get the curl response and store in response variable
response=`(curl  -u $BS_CREDENTIALS $BS_BUILD_STATUS_API)`
echo "API response is `jq -n "$response"`"
#Parse JSON response and get the status field from first element of array
buildStatus=$(jq -n "$response" | jq --raw-output '.[0].automation_build.status')
buildId=$(jq -n "$response" | jq --raw-output '.[0].automation_build.hashed_id')

Upvotes: 1

Logan Lee
Logan Lee

Reputation: 977

Just do

$ jq '.key' <<< $'{"key":"value"}'
"value"

Upvotes: -1

abigail.nguyen
abigail.nguyen

Reputation: 189

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

echo '{"key":"value"}' | jq .key

Upvotes: 10

peak
peak

Reputation: 116640

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"

Upvotes: 42

Supun Madushanka
Supun Madushanka

Reputation: 3238

#! /bin/bash
json_data='{"key":"value"}'
echo $json_data | jq --raw-output '.key'

Upvotes: 5

Wesley Smith
Wesley Smith

Reputation: 19561

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

Upvotes: 2

Javier
Javier

Reputation: 2766

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

Upvotes: 24

jq170727
jq170727

Reputation: 14625

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

jq '.key' <<< "$json_data"

Upvotes: 197

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

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.

Upvotes: 12

Related Questions