1

Suspect it's a newbie mistake that I'm making, but in an attempt to parse a json such as

{"current_observation": {"observation_time": "Last Updated on July 4, 12:53 PM CDT" } }

from within a script such as

#!/bin/sh

jq '.' test.json

I get an error response from ./test.sh as follows

syntax error, unexpected INVALID_CHARACTER, expecting $end '.'1 compile error

Now if I do a jq '.' test.json from the command line in terminal, it performs as expected, but when attempting to use it within a script it fails as detailed.

Anyone able to point out my error?

3
  • bash != sh, try switching the shebang to #!/bin/bash or running it as bash scriptname Commented Jul 5, 2014 at 2:29
  • Your script works fine for me. (jq version 1.4) Commented Jul 5, 2014 at 2:40
  • BroSlow - Made the changes you suggested and ran the script as you detailed. Same error response. Commented Jul 5, 2014 at 8:00

2 Answers 2

2

I can reproduce your error with this script:

#!/bin/sh   
jq \'.\' test.json

I think you just need to get rid of the quotes around the . Although it works fine for me with unescaped quotes.

Try it just like this:

#!/bin/sh   
jq . test.json

It might be related to this bug report for jq. (although probably not as it works for you on the command line)

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

5 Comments

I attempted your escaped quotes suggestion with no change. Though changing the instructions to jq does result in the error response changing to that snippet. As for the underscore bug report, would it present itself only in a script but not if jq was called directly from the command line? As the line of code works perfecty from the command line, just fails as a script.
Yeah I wasn't suggesting you use the escaped quotes. Just saying that it reproduced the error with that. So it's really really weird. Did you try it with just the dot on it's own?
Well well, read your suggestion again and just did away with the quotes completely making the script line jq . test.json and it worked. Any clues as to why the quotes are fine on the line but makes it choke in a script?
No, I couldn't figure that out either. Works fine as you had it originally here on my mac.
I made my answer a bit more explicit in case someone else finds it. Thanks for your question. jq is a cool tool that I didn't know about.
0

Comma produces the error. You should quote it:

{"current_observation": {"observation_time": "Last Updated on July 4\u002c 12:53 PM CDT" } }

Test:

$ jq '.' temp.txt 
{
  "current_observation": {
    "observation_time": "Last Updated on July 4, 12:53 PM CDT"
  }
}

2 Comments

The test.json is not within my control in the end so without already having it parsed to then filter out the commas think I'm screwed down that path. Though did test your suggestion by manually removing the accused comma, but no change in the error response
@user3806826 Try to use the new version of jq which is 1.4.

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.