GithubHelp home page GithubHelp logo

developerstoolbox / validate-json Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 17 KB

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

License: MIT License

Shell 100.00%
wolfsoftware json json-validate

validate-json's Introduction

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 for this purpose and wraps the output into something more consumable within other scripts.

Also see: 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

source validate-json.sh

validate_json "<raw json string>"

From file

source validate-json.sh

validate_json_from_file "<filename>"

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).

source validate-json.sh

validate_json_from_file "<filename>"

Silent Mode

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

silence_messages=true

source validate-json.sh

validate_json_from_file "<filename>"

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

silence_messages=true

source validate-json.sh

if validate_json_from_file "<filename>"; then
    go_perform_some_action_on_data
else
    do_something_else_like_raise_an_error
fi

validate-json's People

Contributors

dependabot[bot] avatar tgwolf avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.