Code Monkey home page Code Monkey logo

json-formatter-validator's Introduction

json-formatter-validator

Build Status Coveralls github GitHub tag (latest SemVer) Sonatype Nexus (Releases) GitHub repo size GitHub code size in bytes GitHub language count GitHub top language GitHub GitHub All Releases GitHub last commit

About json-formatter-validator

Library used to format an invalid JSON: a string in a JSON-like format, but no quotation marks on keys and values, or with other errors. The tool works with two input types: string and JSON file.

The method checkValidityAndFormatObject() checks if JSON is valid or not. If JSON is invalid, the string is formatted via regex to add the quotation marks. If JSON is already valid, nothing is done and JSON itself is returned. This functionality can be called as follows:

Input

String invalidJSON = "{id:267107086801,productCode:02-671070868,lastUpdate:2018-07-15,lastUpdateTimestamp:2018-07-15 01:49:58,payment:[{sequential:1,id:CREDIT_CARD,value:188,installments:9}]}";
CustomJSONFormatter formatter = new CustomJSONFormatter();
formatter.checkValidityAndFormatObject(invalidJSON);  

Output

19/02/14 00:08:57 INFO jsonformattervalidator.JsonFormatterValidatorApplication: Started Json Formatter Validator Aplication
19/02/14 00:08:57 INFO formatter.CustomJSONFormatter: Invalid json: {id: 267107086801,productCode:02-671070868,lastUpdate:2018-07-15,lastUpdateTimestamp:2018-07-15 01:49:58,payment:[{sequential:1,id: CREDIT_CARD,value: 188,installments:9}]}
19/02/14 00:08:57 INFO formatter.CustomJSONFormatter: Valid json: {"id":"267107086801","productCode":"02-671070868","lastUpdate":"2018-07-15","lastUpdateTimestamp":"2018-07-15 01:49:58","payment":[{"sequential":"1","id":"CREDIT_CARD","value":"188","installments":"9"}]}

In versions 2.0 onwards (v.2.0.0, v.2.0.1, etc ...), it is possible to filter one or more attributes of the string that looks like a json or remove one or more attributes, either to discard them by not use or because they are completely corrupted.

Example of the filter option:

Input

String invalidJson = "{id:265998308001,productCode:02-659983080,purchaseDate:2018-01-17,person:{name:Mariana,document:0123456789},deliveryAddress:{street:Rua Fechada,number:666,additionalInfo:casa,city:Lavras,state:MG,zipcode:00000000}}";

String[] filterById = {"id"}; 
String jsonFiltered = formatter.filterJSONObjectsFromString(invalidJson, filterById); 

Output

"{id:265998308001,}"

Example of the remove option:

Input

String invalidJson = "{id:265998308001,productCode:02-659983080,purchaseDate:2018-01-17,person:{name:Mariana,document:0123456789},deliveryAddress:{street:Rua Fechada,number:666,additionalInfo:casa,city:Lavras,state:MG,zipcode:00000000}}";

String[] fieldsToRemove = {"person","deliveryAddress"}; 
String jsonFiltered = formatter.removeJSONObjectsFromString(invalidJson, fieldsToRemove); 

Output

"{id:265998308001,productCode:02-659983080,purchaseDate:2018-01-17,}"

You can also mute the application logs and thrown exceptions. Just use as a parameter true and true values for the muteLog and the muteException variables, respectively, in the method checkValidityAndFormatObject, as the example below:

JsonObject invalidJson = formatter.checkValidityAndFormatObject(jsonFormatted, true, true);

To access the valid JSON, the getValidJson() method should be used. Example:

Input

JsonObject validJson = formatter.getValidJson();
JsonElement id = validJson.get("id");
logger.info(id.getAsLong());

Output

19/02/14 00:08:57 INFO jsonformattervalidator.JsonFormatterValidatorApplication: 267107086801

Dependencies

How to use

You must import .jar into the classpath of your project. If your project is a maven project, just set it as dependency in pom.xml, as follows:

<dependency>
  <groupId>io.github.mariazevedo88</groupId>
  <artifactId>json-formatter-validator</artifactId>
  <version>2.0.6</version>
</dependency>

Contributing

json-formatter-validator's People

Contributors

dependabot[bot] avatar mariazevedo88 avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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