Giter VIP home page Giter VIP logo

json-command's Introduction

JSON Command

JSON command line processing toolkit.

no more writing code to inspect or transform JSON objects.

this npm module includes a json shell command. the json command processes standard input and parses json objects. json currently handles a few different standard input formats and provides a number of options tailored toward inspecting and transforming the parsed json objects.

IMPORTANT

json-command has been renamed to json in npm. all updates will be made to the json npm package.

you need node v0.4.0 or higher and npm to run this program.

Installation

npm install json

installs json shell command.

Examples

the following examples parse json output from twitter http requests and output the requested json fields.

please note that the last two examples require you to enter your username and password.

curl http://search.twitter.com/search.json?q=node.js

curl -s http://search.twitter.com/search.json?q=node.js | json

curl -s http://search.twitter.com/search.json?q=node.js | json -o results

curl -s http://search.twitter.com/search.json?q=node.js | json -o results from_user metadata

curl -s http://search.twitter.com/search.json?q=node.js | json -o results new_id=id

curl -s http://search.twitter.com/search.json?q=node.js | json -o results -C from_user from_user_id

curl -s http://stream.twitter.com/1/statuses/sample.json -uAnyTwitterUser:Password | json user.name user.id

curl -s http://stream.twitter.com/1/statuses/sample.json -uAnyTwitterUser:Password | json user.name user.id -c "entities.user_mentions.length > 0"

Synopsis

json [options] [fields]

Options

-h                    print help info and exit

-v (-V | --version)   print version number and exit

-u                    print ugly json output, each object on a single line

-d                    print debugging output including exception messages

-o object.path        specify the path to an array to be iterated on

new.key=old_key       move old_key to new.key in output object

-a                    input object is an array, process each element separately

-c "js conditional"   js conditional to be run in the context of each object that determines whether an object is printed

-C                    print the output fields as tab delimited columns in the order specified by fields

-e "js expression"    execute arbitrary js in the context of each object.

-i                    use node's util.inspect instead of JSON.stringify.

-H                    print headers, if they are supplied. Useful for output from curl -i.

Fields

any number of fields can be specified to be printed from each json object. by default the structure of the original json object is maintained, however options like -e and foo=bar allow for transforming object structure.

to remove a particular key, assign it to undefined using the -e flag.

Standard Input Formats

  • line delimited json objects
  • back to back json objects. e.g. { obj1 : body }{ obj2 : body }
  • file separated json objects. e.g. cat files/* | json
  • when using -a, a single JSON array

Order of operations

  1. objects are parsed from stdin

  2. any non-existing requested keys are instantiated

  3. key transforms are applied to the object

  4. expressions are run against the object

  5. conditionals are checked against the object. the object is discarded if conditionals do not pass

  6. the requested keys are pulled from the object and output

Limitations

  • all input is handled through standard in
  • the current version is not particularly slow, but is NOT optimized for speed in any way

Documentation

the npm package includes a man page that can be accessed using:

man json

Known Issues

this is alpha quality code, so use at your own risk.

Thanks

this project respectfully uses code from and thanks the authors of:

json-command's People

Contributors

andreyvit avatar isaacs avatar johan avatar tricknotes avatar zpoley 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  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  avatar  avatar

Watchers

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

json-command's Issues

Install instructions

I already have json in my PATH.

After running npm install json, no files are added to my current path nor does $ json run your program.

Could you please explain how to use your product in this situation.

pygmentize: command not found

$ json -h
-bash: pygmentize: command not found
No JSON object could be decoded

Even when I install -g pygmentize, it still complains.

Is pygmentize required?

$ npm list -g
/usr/local/share/npm/lib
└── [email protected]

$ node --version
v0.10.5

0 value parsed as null

Simple example:

$ echo '{"memory":11744,"messages":0}' | json memory
{
"memory": 11744
}

but:
$ echo '{"memory":11744,"messages":0}' | json messages
{
"messages": null
}

This is especially confusing when you what to parse a number using -C option.
$ echo '{"memory":11744,"messages":0}' | json -C messages

you get nothing as result. just empty line when 0 was expected.

Syntax errors when parsing

Hi there.

I've come across errors when processing json from a couchdb. I'll use the standard _users database for example.

$ curl "localhost:5984/_users/_all_docs" 
{"total_rows":1,"offset":0,"rows":[
{"id":"_design/_auth","key":"_design/_auth","value":{"rev":"1-c44fb12a2676d481d235523092e0cec4"}}
]}

Now when sending through json, this is the output:

     $ curl "localhost:5984/_users/_all_docs" | json
 {
    "id": "_design/_auth",
    "key": "_design/_auth",
    "value": {
        "rev": "1-c44fb12a2676d481d235523092e0cec4"
    }
}

Here's the debugging output:

$ curl "localhost:5984/_users/_all_docs" | json -d
ex: {
    "stack": "SyntaxError: Unexpected end of input\n    at Object.parse (native)\n    at [object Object].processObjects (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:351:31)\n    at Socket.<anonymous> (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:443:40)\n    at Socket.emit (events.js:64:17)\n    at Socket._onReadable (net.js:667:31)\n    at IOWatcher.onReadable [as callback] (net.js:177:10)",
    "arguments": [],
    "type": "unexpected_eos",
    "message": "Unexpected end of input"
}
{
    "id": "_design/_auth",
    "key": "_design/_auth",
    "value": {
        "rev": "1-c44fb12a2676d481d235523092e0cec4"
    }
}
ex: {
    "stack": "SyntaxError: Unexpected token ]\n    at Object.parse (native)\n    at [object Object].processObjects (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:351:31)\n    at Socket.<anonymous> (/usr/local/lib/node/.npm/json/0.0.8/package/lib/jsonCommand.js:448:18)\n    at Socket.emit (events.js:61:17)\n    at Socket._onReadable (net.js:652:51)\n    at IOWatcher.onReadable [as callback] (net.js:177:10)",
    "arguments": [
        "]"
    ],
    "type": "unexpected_token",
    "message": "Unexpected token ]"
}

json doesn't integrate with piping commands

freundt@clyde:pts/10:~> json -o data -u < /tmp/foo | head
7.26
7.475
7.47
7.575
7.35
7.35
7.575
7.41
7.55
7.63

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EPIPE, Broken pipe
    at Socket._writeImpl (net.js:159:14)
    at Socket._writeOut (net.js:444:25)
    at Socket.write (net.js:377:17)
    at [object Object].processObjects (/home/freundt/usr/lib/jsonCommand.js:388:24)
    at [object Object].<anonymous> (/home/freundt/usr/lib/jsonCommand.js:443:40)
    at [object Object].emit (events.js:64:17)
    at [object Object]._emitData (fs.js:914:29)
    at afterRead (fs.js:898:10)
    at wrapper (fs.js:245:17)

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.