Code Monkey home page Code Monkey logo

json-merge's Introduction

json-merge

given multiple streams of newline delimited JSON data perform a merge/extend on each object in the stream

NPM dat

usage

npm install json-merge -g
Usage: json-merge <source1> [options] <source2> [options] [<source3>...]

Options
--parse=<s>	 Parse the precedent source with <s>

sources can be one of 3 things:

  • paths to newline-delimited json data files
  • HTTP uris that return newline-delimited json data
  • JSONStream selector syntax to be used to parse stdin into a stream of JS objects

examples

newline delimited json files

e.g. given a file a.json:

{"foo": "bar"}

and a file b.json:

{"taco": "pizza"}

then running:

json-merge a.json b.json

would output:

{"foo": "bar", "taco": "pizza"}

JSONStream parsing

you can also specify JSONStream query syntax and json-merge will parse stdin

e.g. if foobar.json is:

{
  "data": [
    {
      "a": {
        "all": "lowercase"
      },
      "b": {
        "ALL": "CAPS"
      }
    }
  ]
}

then running:

cat foobar.json | json-merge "data.*.a" "data.*.b"

would output:

{"all":"lowercase","ALL":"CAPS"}

Parsing different sources

A given a file food1.json:

{
  "salty": 
    {
      "tacos": "muybien",
      "pretzel": "jawohl"
    }
}

and a file food2.json:

{
  "sweet":
    {
      "waffle": "delicious",
      "pancake": "yummy"
    }
}

then running:

json-merge food1.json --parse="salty" food2.json --parse="sweet"

would output:

{"tacos":"muybien","pretzel":"jawohl","waffle":"delicious","pancake":"yummy"}

json-merge's People

Contributors

finnp avatar max-mapper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

json-merge's Issues

Import from other commands

I have situations where I would like to merge the output of different commands. I am not sure yet how to solve this nicely.

This is an idea of how to do this:

json-merge --run="gasket run import" --run="curl http://awesomedata.org/bestset.ldjson"

Trouble with newline

I am on fedora 24 using nodejs 4.4.6.

When I use json merge on files like the example:

{"foo": "bar"}
{"taco":"pizza"}

it works! But when I enter a newline like this:

{
"foo": "bar"}

I get this error:

undefined:1
{


SyntaxError: Unexpected end of input
    at Object.parse (native)
    at DestroyableTransform.strict (/usr/lib/node_modules/json-merge/node_modules/ldjson-stream/index.js:14:26)
    at DestroyableTransform.transform [as _transform] (/usr/lib/node_modules/json-merge/node_modules/ldjson-stream/node_modules/split2/index.js:27:21)
    at DestroyableTransform.Transform._read (/usr/lib/node_modules/json-merge/node_modules/ldjson-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (/usr/lib/node_modules/json-merge/node_modules/ldjson-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/usr/lib/node_modules/json-merge/node_modules/ldjson-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/usr/lib/node_modules/json-merge/node_modules/ldjson-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at DestroyableTransform.Writable.write (/usr/lib/node_modules/json-merge/node_modules/ldjson-stream/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:194:11)
    at ReadStream.ondata (_stream_readable.js:536:20)
    at emitOne (events.js:77:13)

Unexpected end of input

I have installed json-merge via npm. When I execute it in the same directory where my json files are, using json-merge a.json b.json I get:

`undefined:1
{

SyntaxError: Unexpected end of input
at Object.parse (native)
at DestroyableTransform.strict (C:\Users\dev\AppData\Roaming\npm\node_modules\json-merge\node_modules\ldjson-stream\index.js:14:26)
at DestroyableTransform.transform [as _transform] (C:\Users\dev\AppData\Roaming\npm\node_modules\json-merge\node_modules\split2\index.js:27:21)
at DestroyableTransform.Transform._read (C:\Users\dev\AppData\Roaming\npm\node_modules\json-merge\node_modules\readable-stream\lib_stream_transform.js:184:10)
at DestroyableTransform.Transform._write (C:\Users\dev\AppData\Roaming\npm\node_modules\json-merge\node_modules\readable-stream\lib_stream_transform.js:172:12)
at doWrite (C:\Users\dev\AppData\Roaming\npm\node_modules\json-merge\node_modules\readable-stream\lib_stream_writable.js:237:10)
at writeOrBuffer (C:\Users\dev\AppData\Roaming\npm\node_modules\json-merge\node_modules\readable-stream\lib_stream_writable.js:227:5)
at DestroyableTransform.Writable.write (C:\Users\dev\AppData\Roaming\npm\node_modules\json-merge\node_modules\readable-stream\lib_stream_writable.js:194:11)
at ReadStream.ondata (_stream_readable.js:536:20)
at emitOne (events.js:77:13)`

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.