Skip to content

Programmatically replace fragments of JSON while keeping the overall structure

Notifications You must be signed in to change notification settings

python-lapidary/betterer-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Betterer-JSON

Make Your JSON Great Again

What

Betterer-json allows users to programmatically edit (and hopefully make better-er) JSON files using JavaScript and JSONPath. It applies JSONPath to select multiple values and calls JavaScript functions to generate fixes.

How

Betterer-json executes a user-provided JS script with one or more calls to replace()

replace function accepts JSONPath and callback.

  • JSONPath select any number of values from the input JSON;
  • callback is called for each of those values and its result is used as its replacement.

faulty.json:

{
  "type": "string",
  "format": "datetime"
}

fix.js

/// replace 'format: datetime' with 'format: date-time'
replace('$..format', value => {
    if(value === 'datetime')
        return 'date-time'
    else return value;
});

better.json:

{
  "type": "string",
  "format": "date-time"
}
npx betterer-json fix.js faulty.json > better.json

Why

Third party JSON documents often don't conform to their declared JSON Schema, which causes problems with processing them down the line. Updating them is a repetitive task, more so If these documents are updated often.

Most other JSON tools focus more on reporting or extracting data. The second-best is JSON Patch, but it changes documents one value at a time.

Credits

This project is just a fancy wrapper over a great JSON P3.

About

Programmatically replace fragments of JSON while keeping the overall structure

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published