json2yaml.js/README.md

112 řádky
1.9 KiB
Markdown
Surový Trvalý odkaz Normální zobrazení Historie

2018-09-22 19:52:20 +00:00
Unmaintained Mirror
===============
Please go to the real repo at <https://git.coolaj86.com/coolaj86/json2yaml.js>
2013-07-28 03:51:29 +00:00
json2yaml
2012-01-30 20:18:18 +00:00
===
2013-07-28 03:51:29 +00:00
A command-line utility to convert JSON to YAML (meaning a `.json` file to a `.yml` file)
2012-01-30 20:18:18 +00:00
The purpose of this utility is to pretty-print JSON in the human-readable YAML object notation
2013-07-28 02:58:44 +00:00
(ignore the misnomer, YAML is not a Markup Language at all).
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
Installation
===
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
```bash
npm install -g json2yaml
```
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
*Note*: To use `npm` and `json2yaml` you must have installed [NodeJS](http://nodejs.org#download).
2012-01-30 20:18:18 +00:00
Usage
---
Specify a file:
2013-07-28 02:58:44 +00:00
```bash
json2yaml ./example.json > ./example.yml
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
yaml2json ./example.yml | json2yaml > ./example.yml
```
2012-01-30 20:18:18 +00:00
Or pipe from stdin:
2013-07-28 02:58:44 +00:00
```bash
curl -s http://foobar3000.com/echo/echo.json | json2yaml
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
wget -qO- http://foobar3000.com/echo/echo.json | json2yaml
```
2012-01-30 20:18:18 +00:00
Or require:
2013-07-28 02:58:44 +00:00
```javascript
(function () {
"use strict";
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
var YAML = require('json2yaml')
, ymlText
;
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
ymlText = YAML.stringify({
"foo": "bar"
, "baz": "corge"
});
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
console.log(ymlText);
}());
```
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
Example
===
So, for all the times you want to turn JSON int YAML (YML):
```javascript
{ "foo": "bar"
, "baz": [
"qux"
, "quxx"
]
, "corge": null
, "grault": 1
, "garply": true
, "waldo": "false"
, "fred": "undefined"
}
```
2012-01-30 20:18:18 +00:00
2013-07-28 02:58:44 +00:00
becomes
```yaml
---
foo: "bar"
baz:
- "qux"
- "quxx"
corge: null
grault: 1
garply: true
waldo: "false"
fred: "undefined"
```
*Note*: In fact, both of those Object Notations qualify as YAML
because JSON technically *is* a proper subset of YAML.
That is to say that all proper YAML parsers parse proper JSON.
YAML can use either *whitespace and dashes* or *brackets and commas*.
For human readability, the whitespace-based YAML is preferrable.
2013-07-28 02:59:51 +00:00
For compression and computer readability, the JSON syntax of YAML is preferrable.
2013-07-28 03:51:29 +00:00
Alias
===
`json2yaml` has the following aliases:
* `jsontoyaml`
* `json2yml`
* `jsontoyml`