# any-json

> Convert (almost) anything to JSON.

Latest version **3.1.0** (published 2017-08-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install any-json
pnpm add any-json
yarn add any-json
bun add any-json
```

Provides the command `any-json`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2017-08-28 |
| First published | 2015-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 16 |
| Known vulnerabilities | 0 (+8 in 4 direct dependencies) |
| Install scripts | no |
| GitHub stars | 147 |
| Author | Christian Zangl |
| Maintainers | adamvoss, laktak |
| Keywords | json, hjson, yaml, cson, json5, xml, ini |

## Links

- npm: https://www.npmjs.com/package/any-json
- Repository: https://github.com/any-json/any-json
- Issues: https://github.com/any-json/any-json/issues
- npm.io page: https://npm.io/package/any-json

## Dependencies (16)

- [ini](https://npm.io/package/ini.md) ^1.3.4
- [cson](https://npm.io/package/cson.md) ^4.1.0
- [xlsx](https://npm.io/package/xlsx.md) ^0.11.3
- [hjson](https://npm.io/package/hjson.md) ^2.4.1
- [json5](https://npm.io/package/json5.md) ^0.5.1
- [xlsjs](https://npm.io/package/xlsjs.md) ^0.7.5
- [xml2js](https://npm.io/package/xml2js.md) 0.4.17
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.7.0
- [dashdash](https://npm.io/package/dashdash.md) ^1.14.1
- [fast-csv](https://npm.io/package/fast-csv.md) ^2.4.0
- [toml-j0.4](https://npm.io/package/toml-j0.4.md) ^1.0.10
- [typescript](https://npm.io/package/typescript.md) ^2.4.2
- [tomlify-j0.4](https://npm.io/package/tomlify-j0.4.md) ^2.1.1
- [format-unicorn](https://npm.io/package/format-unicorn.md) ^1.1.0
- [util.promisify](https://npm.io/package/util.promisify.md) ^1.0.0
- [strip-json-comments](https://npm.io/package/strip-json-comments.md) ^2.0.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.1.0 (latest) — 2017-08-28
- 3.0.0 — 2017-08-04
- 2.2.0 — 2017-01-18
- 2.1.0 — 2016-08-08
- 2.0.0 — 2016-04-29
- 1.0.1 — 2015-08-25
- 1.0.0 — 2015-05-27
- 0.3.2 — 2015-02-18
- 0.3.1 — 2015-02-18
- 0.3.0 — 2015-02-18
- 0.2.0 — 2015-02-04
- 0.1.1 — 2015-02-03
- 0.1.0 — 2015-02-03

## README

# any-json

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

[![NPM version](https://img.shields.io/npm/v/any-json.svg?style=flat-square)](http://www.npmjs.com/package/any-json)

## Install from npm

```
npm install any-json -g
```

## Examples

### convert

```bash
# 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

```bash
# 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

```bash
# 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
  - It cannot parse its own output [node-xml2js#391](https://github.com/Leonidas-from-XIV/node-xml2js/issues/391)

### 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

```js
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](https://github.com/any-json/any-json/releases)._

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

---
_Source: https://npm.io/package/any-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
