Search code examples
bashcurljqquoting

Brace usage error while using curl with in bash


Following curl usage is not liking braces inside braces. I tried escaping but it didnt help ... any ideas?

echo "$(curl -s -u ${USERNAME}:${PASSWORD} GET ${hostName}/${path}.json| jq -r -c '[.field1,.field2] | \"(.[0]) ,(.[1])"')"

Result:

jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell
quoting issues?) at <top-level>, line 1:

error near (.[0]`)`

Solution

  • To debug cases like this, it's best to break things down into the basic components first. In your case, I'd guess you intended the jq filter to be:

    [.field1,.field2] | "\(.[0]), \(.[1])"
    

    To test:

     jq -r -c '[.field1,.field2] | "\(.[0]), \(.[1])"'
     {"field1": 1, "field2": 2}
     1, 2
    

    Once you have that part right, the rest is easy. Assuming you are using a decent shell, you could probably make life simple for yourself by using $() rather than "$()", or avoiding command substitution altogether.

    From the development and testing point of view, it might make sense to put the jq program that you know to be correct into a file, say program.jq, and then you can in sequence:

    (a) verify it in stand-alone mode, using jq -r -c -f program.jq

    (b) plug the jq invocation into your pipeline to verify that there isn't another problem elsewhere.

    If using program.jq isn't ultimately satisfactory, you might want to consider setting a shell variable to the jq program, e.g.

    JQ='[.field1,.field2] | "\(.[0]), \(.[1])"'
    
    echo $(jq -r -c "$JQ")
    

    Of course the last line above is just there for testing. Maybe you don't even need $() at all?