Giter Club home page Giter Club logo

any-json's Introduction

any-json

any-json can be used to convert (almost) anything to and from JSON.

NPM version

Install from npm

npm install any-json -g

Examples

convert

# Prints as JSON to standard out
any-json package.json5

# Writes the contents from `package.json` to `package.json5` as JSON5
any-json package.json package.json5

# Same as above (the `convert` command is default).
any-json convert package.json5
any-json convert package.json package.json5

combine

# Prints an JSON array containing an item for every JSON file in directory
any-json combine *.json

# Combines A.json and B.json, writing the result to C.json
any-json combine A.json B.json --out C.json

# Create a csv from a collection of flat YAML files
any-json combine *.yaml --out=data.csv

split

# Creates a JSON file for each row in the CSV where the name is based on the `product_id` column
any-json split products.csv prod-{product_id}.json

Formats and Data Safety

Safe

When only JSON features are used, conversion should not result in any data loss when using these formats.

  • cson
  • hjson
  • json
  • json5
  • yaml

Problematic

Some loss of information may occur. Improved parsers/serializers could provide better compatibility, but implementation is provided as-is. Known issues are listed below the format.

  • ini
    • All numbers are converted to strings (the ini library would need to quote strings in order be recognize numbers)
    • In the unlikely case an object was not an array but has only has sequential, numeric members starting at zero it will be decoded as an array.
  • toml
    • Top-level array and date objects are not supported
      • It would be non-standard, but this could be worked around by wrapping the content in an object (then discarding the object when decoding).
  • xml

Limited

These formats are conceptually different and only work on a limited basis. With effort, conventions could be established to provide a more complete transfer but there will be some impedance.

Tabular formats:

  • csv
  • xls
  • xlsx

Usage

Command Line

usage: any-json [command] FILE [options] [OUT_FILE]

any-json can be used to convert (almost) anything to JSON.

This version supports:
    cson, csv, hjson, ini, json, json5, toml, yaml

This version has is beta support for:
    xls, xlsx

The beta formats should work, but since they are new,
  behavior may change in later releases in response to feedback
  without requiring a major version update.

command:
    convert    convert between formats (default when none specified)
    combine    combine multiple documents
    split      spilts a document (containing an array) into multiple documents

options:
    -?, --help              Prints this help and exits.
    --version               Prints version information and exits.
    --input-format=FORMAT   Specifies the format of the input (assumed by file
                            extension when not provided).
    --output-format=FORMAT  Specifies the format of the output (default: json or
                            assumed by file extension when available).

  combine (additional options):
    --out=OUT_FILE          The output file.

API

const anyjson = require('any-json')
const obj = await anyjson.decode(/* string to parse */, /* format (string) */)
const str = await anyjson.encode(/* object to encode */, /* desired format (string) */)

Contributing

Contributions welcome! If any-json is not meeting your needs or your have an idea for an improvement, please open an issue or create a pull request.

History

For detailed release history, see Releases.

  • v3 Re-written to be Promise-based and bi-directional (serialization capabilities as well as parsing).
  • v2 removed the experimental/unreliable format detection.

any-json's People

Contributors

adamvoss avatar laktak avatar marsonya 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

any-json's Issues

Parsing an array returns only last element

If I have the following string:

Label: ISO Speed
Type: RADIO
Current: 200
Choice: 0 200
Choice: 1 250
Choice: 2 320
Choice: 3 400
Choice: 4 500
Choice: 5 640
Choice: 6 800
Choice: 7 1000
Choice: 8 1250
Choice: 9 1600

any-json -format=yaml will return

{
  "Label": "ISO Speed",
  "Type": "RADIO",
  "Current": 200,
  "Choice": "9 1600"
}

So only last element will return, but is there any other option to set to return it complete ?
Best regards,
Nic

autodetect CSV separator in input

the converter does not work fine if the input csv is not using commas as separator.

e.g.:

$ curl 'http://beta-service.geonet.org.nz/fdsnws/station/1/query?&format=text&level=channel&network=NZ&starttime=2017-06-01T04:08:18.636' > test.csv
$ any-json convert test.csv test.json

Feature request: stream processing

Are there any plans to add support for reading from standard in and outputting to standard out?

I've created a Docker image to wrap any-json, so it's easier to use in low-dependency scripts, and this would be much easier if there isn't a need to pass files back into and out of the container.

rss

Can this be used as a library?
Does it work with RSS?

white line after section definition

Hi,
If I insert a white line (not empty but with tab or space) after a section in the ini file:

[TEST]
   
toto = 1

result is :

{
    "TEST": {
        "": true,
        "toto": "1"
    }
}

Buffer.js error with large json

Hi,

I am having a 1.4GB .csv file, when trying to convert it to json with any-json I get this error:

bash-4.3# any-json big.csv > big.json
buffer.js:495
    throw new Error('"toString()" failed');
    ^

Error: "toString()" failed
    at Buffer.toString (buffer.js:495:11)
    at Object.fs.readFileSync (fs.js:553:33)
    at Object.<anonymous> (/usr/lib/node_modules/any-json/bin/any-json:52:15)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
bash-4.3# ls -alh

Looks like we are running into something similar to this: https://stackoverflow.com/questions/42181217/buffer-js-error-in-node-json-too-large

Would switching to https://github.com/dominictarr/JSONStream fix it?

Best,
FB

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.