Giter Club home page Giter Club logo

turbo-json.js's Introduction

Turbo 🐡 JSON

Tests Prettier License Bors enabled

turbo-json.js is a tool that combines all json files found in a directory into one big json file using streaming to avoid out-of-memory.

npx turbo-json.js data/
# Outputs `combined.json` file containing the content of all json files found in the data/ directory

All json files found in data directory will be combined into one file that is named combined.json par default.

Both read and write actions are done using streaming. The maximum data stored in memory is the buffer size.

Table of Contents

Installation

CLI:

Global install to have the CLI accessible from everywhere on your operating system.

npm install -g turbo-json.js # install globaly

No installation needed when using npx.

npx turbo-json.js [options]

Library:

# yarn
yarn add turbo-json.js

# npm
npm install turbo-json.js

Usage

turbo-json.js takes one argument:

  • Input directory: the path to the directory containing all the jsons.

and options:

  • Output file optional : path to the output file (default: combined.json).
  • Validate Input JSON's: All input JSON files are validated to be a correct JSON (default: true).
  • Validate Output JSON's: Validate if the outputed JSON is a correct JSON (default: false).
  • Quiet mode : no logs are outputed (default: false).

It accepts relative path but also fully qualified paths.

CLI usage:

Usage:

turbo-json [options] <input-directory>

Options:

  -o, --output-file <file-name>      File name in which all the json files will be merged (default: "combined.json")
  -I, --validate-input <file-name>   Check if input JSON files are valid (default: true)
  -O, --validate-output <file-name>  Check if output JSON is a valid JSON (default: false)
  -q, --quiet                        Quiet mode, no logs are outputed (default: false)

Example

turbo-json /data -o combined_data.json

Library usage

const { combineJson } = require('./src');

(async () => {
  await combineJson('misc', { outputFile: 'combined_data.json', validateInput: true, validateOutput: false, quiet: false });
})();

How is it combined

The tool requires the path to a directory to work. Inside that directory all json files are read and merged into one big json file.

The JSON file outputed by this tool contains an array with all the inputed JSON files.

Example:

If your JSON file contained { "id": 1 } it will be stored in the output file like this [{"id": 1}]

Some examples:

Input files:

{ "id": 1 }
{ "id": 2 }

Output file:

[
  { "id": 1 },
  { "id": 2 }
]

Array exception: There is one exception to this rule. If your JSON file contains an array, it will be deconstructed/flattened in the final file (could become an option please make an issue if you'd like that).

Input files:

[ 1, 2, 3 ]
{ "id": 1 }

Output file:

[
  1,
  2,
  3,
  { "id": 1 }
]

Validate Json Format

By default the tool supposes that your JSON files are correctly formated. If you'd like to check if it is well formated before concatenation the validateInput options should be set to true. By doing so, only the well formated JSON's are concatenated but this comes at the price that every file will be stream-read two times. Once for validation and once for writing.

The same is possible to validate the output file using validateOutput.

Validity is based on the description provided by json.org.

Example

Using the JSON files present in misc, you can observe the outputed file misc_output.json

At the root of the repository use the following:

npx turbo-json.js misc

it will generate a combined.json file with all the JSON objects found in misc.

turbo-json.js's People

Contributors

bidoubiwa avatar bors[bot] avatar dependabot[bot] avatar

Stargazers

Jbraczyk avatar Joshua Belke avatar Nicolas Viénot avatar

Watchers

James Cloos avatar  avatar

Forkers

jbelke

turbo-json.js's Issues

Recursively take all document in directory

For the moment only the files at the root of the provided directory will be combined. We should provide a way to use the bash pattern syntax to provide the list of json documents

Bug on node 12

For some reason when running the script in node 12 the events of the stream-json library are not waited on.

The following works well with node 14 > but not with node 12.

  await new Promise(function (resolve, rejects) {
    verifierStream.on('close', function () {
      resolve();
    });
    verifier.on('error', error => {
      rejects({message: `Json file is not valid: ${jsonFile}: ${error.message}`, file: jsonFile, error: error.message})
    });
  });

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.