Giter VIP home page Giter VIP logo

yaml2json's Introduction

yaml2json

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

The purpose of this utility is to minify YAML as JSON. (ignore the misnomer, YAML is actually an Object Notation, not a Markup Language)

Installation

npm install -g yaml2json

Usage

Specify a file:

yaml2json ./example.yml

json2yaml ./example.json | yaml2json

Or pipe from stdin:

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

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

Example

---
  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: JSON is a proper subset of YAML. The difference is that YAML can use whitespace instead of syntax, which is more human-readable. Also, YAML supports comments.

Alias

yaml2json has the following aliases:

  • yml2json
  • yamltojson
  • ymltojson

yaml2json's People

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

Watchers

 avatar  avatar  avatar  avatar

yaml2json's Issues

Standard Input Pipes?

Great little utility. Went to feed it a yaml stream on stdin from a bash unix pipe and noticed not setup to read stdin. Did I miss something? If not, would you mind if I setup a pull request to add that functionality? (I'll wait to start the work to hear back to know if that is something you already intended or I missed.)

Brings up a bigger question about any standards or best practices for creating node command line utilities in general. Some commands take -- to indicate stdin coming and others just deal with it if there is something on the stdin pipe to read from.

yaml2json is parsing just a single line-- exits without erro

Hi,

I am using yaml2json for the first time. My OS is Windows 7 and I am using git bash.
May be I am missing something very basic, can you guys please help/guide me here.

I tried sending the output of the bash text processing command to test.yml and I can see the test.yml file is created properly. But once I feed it as a input to yaml2json, it parses just the first line "version" :1 and exits without any error.

However, If I try to convert test.yml file contents online via site:--http://yamltojson.com/-- the resulting .json is proper.

Following are the contents of test.yml file generated:--

version: 1
layout: post
lang: en
slug: "checklist"
type: "modal"
title: "Checklist"
published: "true"
categories: "mobile"
tags: "mobile"
action:

  • title: "Disguise Now"
    link: "close"
  • title: "Cancel"
    link: "home-ready"
    status: disabled
    checklist:
  • title: "Review security plan and update contacts regularly"
  • title: "Test the alert to ensure it is working"
  • title: "Ensure sufficient credit on the phone"
  • title: "Activate GPS (if you want to enable location tracking)"
  • title: "Charge phone battery to full"

yaml2jsonconversionimproper
yaml2jsonconversiononline

json2yaml and yaml2json back

echo '{"gti-dev":{"description":"development environment"},"virtualization":"HyperV","host OS":"ESXi"}' | json2yaml | yaml2json
/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:179
throw new Error(msg + ', ' + context(this.peek()[1].input))
^

Error: document not properly dedented, near "host OS: "ESXi"\n\n"
at Parser.expect (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:179:9)
at Parser.parseDoc (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:266:8)
at Parser.parse (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:232:19)
at Object.exports.eval (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:384:46)
at handleInput (/usr/local/lib/node_modules/yaml2json/index.js:31:17)
at Socket. (/usr/local/lib/node_modules/yaml2json/index.js:80:9)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at doNTCallback2 (node.js:450:9)

yaml2json chokes on multi-part documents

Attempt to convert even one of the simplest yaml documents failed instantily because when it encounters the --- document separator it chokes.

---
# that is a valid yaml document and some linters are even requiring us to add the --- separator on the top.
foo: var

Parse Error

Receive an error when using yaml2json to parse the following. Online parsers work fine. Cannot find any similar issues online. I also tried a yaml file that had the strings quoted but got the same issue.

YAML:


---
  foo: bar
  baz:
    - qux
    - quxx
  corge: null
  grault: 1
  garply: true
  waldo: "false"
  fred: undefined

CLI

$ yaml2json ./example.yml

CONSOLE ERROR

/Users/user/.nvm/v0.10.29/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:179
  throw new Error(msg + ', ' + context(this.peek()[1].input))
                                                  ^
TypeError: Cannot read property '1' of undefined
    at Parser.expect (/Users/user/.nvm/v0.10.29/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:179:51)
    at Parser.parseDoc (/Users/user/.nvm/v0.10.29/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:266:8)
    at Parser.parse (/Users/user/.nvm/v0.10.29/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:232:19)
    at Object.exports.eval (/Users/user/.nvm/v0.10.29/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:384:46)
    at handleInput (/Users/ben/.nvm/v0.10.29/lib/node_modules/yaml2json/index.js:31:17)
    at /Users/user/.nvm/v0.10.29/lib/node_modules/yaml2json/index.js:56:9
    at fs.js:271:14
    at Object.oncomplete (fs.js:107:15)

Failing to convert on Ubuntu 14.04

Input

--- 
- 
  instrument: "Lasik 2000"
  pulseDuration: 12
  pulseEnergy: 5.4
  repetition: 1000
  spotSize: 1mm
- 
  instrument: "Lasik 2000"
  pulseDuration: 10
  pulseEnergy: 5.0
  repetition: 500
  spotSize: 2mm

Output:

/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:179
  throw new Error(msg + ', ' + context(this.peek()[1].input))
        ^
Error: expected indent after document, near " \n- \n  instrument: \"Lasik"
    at Parser.expect (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:179:9)
    at Parser.parseDoc (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:264:8)
    at Parser.parse (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:232:19)
    at Object.exports.eval (/usr/local/lib/node_modules/yaml2json/node_modules/yaml/lib/yaml.js:384:46)
    at handleInput (/usr/local/lib/node_modules/yaml2json/index.js:31:17)
    at /usr/local/lib/node_modules/yaml2json/index.js:56:9
    at fs.js:268:14
    at Object.oncomplete (fs.js:107:15)

YAMLint.com says it's valid YAML, and conversion works on http://yamltojson.com/

Keep whitespaces and structure

Yaml:

bla:
  yo: [1,2,3,4,5,6]
  foo: bar
  baz: {keep: me, one: lined}

Expected (roughly):

{ "bla":
  { "yo": [1, 2, 3, 4, 5, 6],
    "foo": "bar",
    "baz": {"keep": "me", "one": "lined"}}}

But got:

{
  "bla": {
    "yo": [
      1,
      2,
      3,
      4,
      5,
      6
    ],
    "foo": "bar",
    "baz": {
      "keep": "me",
      "one": "lined"
    }
  }
}

Thoughs?

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.