0

Looking for a shell command /code to validate json input file (if all characters {{[] are present) using regex. Especially end of the file data is ]} as my file json data ends with those characters.

Found we can do it with awk or sed, but unable to get command working on shell.

Thanks for the help!

1
  • 1
    Using regular expressions is simply not feasible. There are theoretical reasons why you cannot solve this problem with a regex. Commented Mar 29, 2019 at 16:22

2 Answers 2

2

To validate a JSON file, use a proper tool/parser: .

#!/bin/bash

jq file.json &>/dev/null || echo >&2 "invalid JSON"
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, OS is redhat 5.xx . I can't install any new tools for security reasons. jq is not present on the system. Any other way ?
0

If you do not want to install any other tool but if you have php installed you can use php's json parser from command line

php -r "if ( json_decode(file_get_contents('file.json')) == null ) { echo 'Invalid Json Data'; } else { echo 'Valid Json File'; } "

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.