9
cat explorer/connection-profile/test-network.json | jq ".organizations.Org1MSP.adminPrivateKey.path |= 44ab"

jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.organizations.Org1MSP.adminPrivateKey.path|=44ab                                               
jq: 1 compile error

but it works fine with

cat explorer/connection-profile/test-network.json | jq ".organizations.Org1MSP.adminPrivateKey.path |= 44"

Why?

Actually I am trying to use

cat explorer/connection-profile/test-network.json | jq ".organizations.Org1MSP.adminPrivateKey.path |= ${PRIV_KEY}"

where the ${PRIV_KEY} is 44ab..._sk

2
  • Try ... | jq '.organizations.Org1MSP.adminPrivateKey.path |= "44ab"' Commented Sep 2, 2021 at 9:16
  • @jas Yeah, this works fine, but actually I want change 44ab with a variable, so I cant use double quote ('') outside this string. ^ ^ thanks anyway Commented Sep 2, 2021 at 9:31

3 Answers 3

11

You can assign a string to a variable that can be used inside a jq filter:

PRIV_KEY="44ab..._sk"
jq --arg path "$PRIV_KEY" '.organizations.Org1MSP.adminPrivateKey.path |= $path' explorer/connection-profile/test-network.json 

This method is safer than trying to embed an expanded shell variable directly in the filter string because jq will properly handle arbitrary values instead of choking on things like quotes (Or their absence).


Note that jq takes filenames as arguments after the filter expression; no need for cat here (Unless that's standing in for curl or something, of course, and you're not using an existing file)

Sign up to request clarification or add additional context in comments.

1 Comment

It works! You are so kind. ^ ^ Thank you!
2

did you try this , i fixed the same error by setting the expression between '' and the values between ""

cat explorer/connection-profile/test-network.json | jq '.organizations.Org1MSP.adminPrivateKey.path |= "44ab"'

Comments

0

On the odd chance that anyone else finds this issue as well, it seems that jq doesn't like keys with hyphens in them. That is, if you have a json file with

{"a-1b": [1, 2, 3]}
$ jq '.a-1b' foo.json
jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.a-1b    
jq: 1 compile error

The solution is to quote the key:

$ jq '."a-1b"' foo.json
[
  1,
  2,
  3
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.