Code Monkey home page Code Monkey logo

jme's Introduction

JME CLI

CircleCI Codecov NPM

Fully or partially merge JSON files via the command line.

Installation

npm install jme

Usage

jme file-1.json file-2.json file-3.json > merged.json

file-3.json will be merged with file-2.json and the result will be merged with file-1.json The first file is the target file however, files are never modified, rather the merge result is sent to standard out.

You can pass in any number of files to be merged.

Partial merging

In addition to fully merging json files, you can merge only some of the properties / paths on the JSON object.

Given these files:

// test-1.json
{
  "prop1": {
    "id": 1,
    "tags": [1, 2, 3],
    "level1": {
      "prop1": "t1",
      "customProp1": "t1",
      "level2": { // override this path with the same path from the other files.
        "prop1": "t1",
        "customProp1": "t1"
      }
    }
  }
}


// test-2.json
{
  "prop1": {
    "id": 2,
    "tags": [4, 5, 6],
    "level1": {
      "prop1": "t2",
      "customProp2": "t2",
      "level2": { //merge only this path
        "prop1": "t2",
        "customProp2": "t2"
      }
    }
  }
}

You can choose to merge only a part of the file (in this case prop1.level1.level2)

jme test-1.json test-2.json -p 'prop1.level1.level2'

result:

{
  "prop1": {
    "id": 1,
    "tags": [1, 2, 3],
    "level1": {
      "prop1": "t1",
      "customProp1": "t1",
      "level2": {       //only this path 'prop1.level1.level2' is merged
        "prop1": "t2" // comes from test-2.json
        "customProp2": "t2",// comes from test-2.json
        "customProp1": "t1" // comes from test-1.json
      }
    }
  }
}

You can partially merge multiple paths, just make sure to separate then with comma -p 'prop1.level1.level2,some.other.path,yet.some.other.path'

If the path does not exist in one of the files program will report an error and the merge process will not be executed. However, you can pass an additional flag -u or --allow-undefined to allow undefined paths in the files. In that case, if the path does not exist in the file, that file will be skipped for that particular path.

Full path override

You can also override the path on the last file target ( -o or --override)

In the next example chosen path will be merged from test-3.json into test-2.json and then that same path will be overridden in test-1.json (no merging with the test-1.json)

Example:

jme test-1.json test-2.json test-3.json --path 'prop1.level1.level2' --override

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.