An open API service indexing awesome lists of open source software.

https://github.com/developerstoolbox/validate-json

A bash script that will allow you to validate a JSON string.
https://github.com/developerstoolbox/validate-json

json json-validate wolfsoftware

Last synced: 3 months ago
JSON representation

A bash script that will allow you to validate a JSON string.

Awesome Lists containing this project

README

          



DevelopersToolbox logo




Github Build Status


License


Created




Release


Released


Commits since release















## Overview

This is a very simple tool for validating a JSON string. It utilises [jq](https://stedolan.github.io/jq/) for this purpose and wraps the output into something more consumable within other scripts.

> Also see: [json-lint](https://github.com/CICDToolbox/json-lint) for out travis plugin that performs a similar function.

## Usage

There are 4 different options when it comes to using the validation tool.

1. You can validate a raw json string
2. You can validate the contents of a file
3. You can just make use of error / return codes
4. You can be given error messages as well as return codes

The first 2 options are handled by 2 different exposed functions.

### Raw string

```shell
source validate-json.sh

validate_json ""
```

### From file

```shell
source validate-json.sh

validate_json_from_file ""
```

> This function will load the file contents into a string and then execute validate_json, so the final validation method is exactly the same to ensure identical results.

In additional to the two different wants to pass the data to the validator there are also 2 different ways to get data out of the validator.

### Verbose Mode (default)

Verbose mode will display messages relating to the validity of the string, either everything was ok, or the specific error message returned by jq. It will also use return codes for success (0) and failure (1).

```shell
source validate-json.sh

validate_json_from_file ""
```

### Silent Mode

This mode will suppress all output to the screen and will only use return codes.

```shell
silence_messages=true

source validate-json.sh

validate_json_from_file ""
```

> This is particularly useful if you want to embed the validation into another script and take different actions depending on the result.

#### Embedding Silent Mode

```shell
silence_messages=true

source validate-json.sh

if validate_json_from_file ""; then
go_perform_some_action_on_data
else
do_something_else_like_raise_an_error
fi
```