Giter Site home page Giter Site logo

json2yaml's Introduction

Unmaintained Mirror

Please go to the real repo at https://git.coolaj86.com/coolaj86/json2yaml.js

json2yaml

A command-line utility to convert JSON to YAML (meaning a .json file to a .yml file)

The purpose of this utility is to pretty-print JSON in the human-readable YAML object notation (ignore the misnomer, YAML is not a Markup Language at all).

Installation

npm install -g json2yaml

Note: To use npm and json2yaml you must have installed NodeJS.

Usage

Specify a file:

json2yaml ./example.json > ./example.yml

yaml2json ./example.yml | json2yaml > ./example.yml

Or pipe from stdin:

curl -s http://foobar3000.com/echo/echo.json | json2yaml

wget -qO- http://foobar3000.com/echo/echo.json | json2yaml

Or require:

(function () {
  "use strict";

  var YAML = require('json2yaml')
    , ymlText
    ;

    ymlText = YAML.stringify({
      "foo": "bar"
    , "baz": "corge"
    });

    console.log(ymlText);
}());

Example

So, for all the times you want to turn JSON int YAML (YML):

{ "foo": "bar"
, "baz": [
    "qux"
  , "quxx"
  ]
, "corge": null
, "grault": 1
, "garply": true
, "waldo": "false"
, "fred": "undefined"
}

becomes

---
  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. For compression and computer readability, the JSON syntax of YAML is preferrable.

Alias

json2yaml has the following aliases:

  • jsontoyaml
  • json2yml
  • jsontoyml

json2yaml's People

Contributors

eush77 avatar wayne-foley 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

Watchers

 avatar  avatar  avatar

json2yaml's Issues

Unnecessary linebreaks with array of objects

When the first element of an array is an object, it should't begin with '\n'.

This:
yaml.stringify([{frank: 12, arno: 13},{ben: 211, karl: 21}])

Should return that:

---
  - frank: 12
    arno: 13
  - ben: 211
    karl: 21

Instead of:

---
  - 
    frank: 12
    arno: 13
  - 
    ben: 211
    karl: 21

unsafe-eval error if used with Content Security Policy directive

The remedial dependency that the library is using has these lines of code:

var global = Function('return this')()

This would not execute if 'unsafe-eval' CSP is off.
Any suggestions?
I understand that this is a dependency issue but that project doesn't seem to have a way to report issues/questions.

Thanks,
Diana

yaml2json broken on windows with git-bash (git-scm.org)

Confirmed this is only affecting the git-bash shell on windows, but a lot of people use it.

image

I'll work on a fix, but I think the best fix might be moving to a more standard command-line tool library.

Here's the test.sh failing on windows:

image

Actually you can close this. Looks like the package everyone is using (including Gulp and Grunt) is js-yaml and js-yaml-cli which work on windows bash without a problem.

Remove trailing spaces

The whole file is indent two spaces.
Is it possible to remove those spaces?

So the file will not be longer:

---
  param1:
    name: test

but

---
param1:
  name: test

Even an Option to remove the trailing --- would be nice.

Key starting with @ should be quoted

Converting the following json to yaml:

{
  "dependencies": {
    "@scope": "test"
  }
}

should produce:

dependencies:
  "@scope": test

but currently produces the following (which is invalid):

dependencies:
  @scope: test

The fix for this probably needs to protect against other special characters as well.

Pretty multiline strings

Currently a line with newline characters will be saved as a single line with the newline characters encoded

foo: "1\n2\n3"

However, there are many ways to save a multiline string in the yaml format. Would be nice to support them. Maybe by passing an option.

YAML.stringify({foo: "1\n2\n3"}, {prettyMultiline: true})
//> foo: |
//    1
//    2
//    3

Support for date objects

Right now it just crashes with Error: what the crap: date (lol). Searched around a bit, and though I couldn't find any official date representation on the spec, Symfony suggests ISO-8601 strings, which is a pretty sensible recommendation and a simple one to implement given JS dates have that nice .toISOString() method.

Thoughts?

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.