Score:1

Verify syntax of JSON file from command-line (CLI sanity check)

cn flag

What's the simplest command that I can run on Ubuntu Linux to verify the sanity of a JSON file?

I have a very long JSON file that I just edited with vim. I want to make sure my changes didn't introduce any syntax errors.

I know I could write a python script for this, but I'd prefer something built-in and simpler.

Is there just some bash one-liner that I can run against the file that will tell me if (and where) there's any syntax issues with the JSON file?

Cyrus avatar
cn flag
`jq . file >/dev/null 2>&1 || echo error`?
Score:1
cn flag

You can use jq and send stdout to null

sudo apt-get install jq
jq . /path/to/yourfile.json 1> /dev/null

Example Execution

Here's some example runs

No Errors

If there are no errors in the json file, then there will be no output and the exit code is 0

user@host:~$ jq . updates/v1/meta.json 1> /dev/null
user@host:~$ echo $?
0
user@host:~$

Errors

If there are errors in the json file, then the output will tell you exactly which line & character, and the exit code will be non-zero

user@host:~$ jq . updates/v1/meta.json 1> /dev/null
parse error: Expected another array element at line 15, column 8
user@host:~$ echo $?
4
user@host:~$ 

Score:0
cn flag
sudo apt install jsonlint

and then do jsonlint-php {file}

Manual page:

OPTIONS

   -q, --quiet
          Cause jsonlint to be quiet when no errors are found

   -h, --help
          Show this message
Michael Altfield avatar
cn flag
I installed jsonlint with `sudo apt-get install jsonlint` but I didn't get any new binaries named `jsonlint` (confirmed with `dpkg-query -L jsonlint`) The only new binary that was installed was `jsonlint-php`. This is on Debian 11.
cn flag
ah we changed that indeed. the apt is still correct.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.