Giter VIP home page Giter VIP logo

yaml2json's Introduction

Description

Transform yaml string to json string without the type infomation.

Features

  • zero config.
  • supports Windows, Linux, macOS, FreeBSD, NetBSD, OpenBSD, Plan 9 etc..

Binary installation and usage

  • Download a binary that match your operation system and platform.
  • https://github.com/bronze1man/yaml2json/releases
  • copy it to /usr/local/bin like (cp ~/Downloads/yaml2json_darwin_amd64 /usr/local/bin/yaml2json )
  • Use chmod +x /usr/local/bin/yaml2json give running permission to it.

mac/linux usage

  • echo "a: 1" | yaml2json
  • yaml2json < 1.yml > 2.json

window usage

  • windows 7 cmd.exe:
C:\tmp>more .\1.yaml
a: 1

C:\tmp>.\yaml2json_windows_amd64.exe < ./1.yaml > 2.json

C:\tmp>more .\2.json
{"a":1}
  • windows 7 powerShell 6.1.7600 file example:
PS C:\tmp> more .\1.yaml
a: 1

PS C:\tmp> Get-Content .\1.yaml | .\yaml2json_windows_amd64.exe > .\2.json
PS C:\tmp> more .\2.json
{"a":1}

Library installation

  • go get -v github.com/bronze1man/yaml2json/y2jLib

Development

  • Follow example is for develop on mac and linux. It should work to windows too. (need change some command line and path to work on windows).
  • You need a golang on your computer. https://golang.org
  • Create a new directory as your workspace, like ~/work/yaml2json
  • Change your current work directory to that directory.
cd ~/work/yaml2json
GOPATH=`pwd` go get -v github.com/bronze1man/yaml2json
GOPATH=`pwd` go run github.com/bronze1man/yaml2json/y2jBuilder
  • use the file at $GOPATH/tmp/file to distribute your binary to others.

Notice

  • if you don't know whether your platform is 386 or amd64, use the 386 build...
  • Library api is not easy to use, it will better in next version.

Reference

yaml2json's People

Contributors

bronze1man avatar chocolateboy avatar dolmen avatar ezeeyahoo avatar hohle avatar ialidzhikov 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  avatar

yaml2json's Issues

yaml2json misses underscores in certain strings

It seems the issue is only reproducible for certain strings.
The pattern <number><string><underscore><number> is faulty.
There may be more.

See:

$ cat file.yml
---
property: 0testS_1value
###
$ cat file.yml | yaml2json
{"property":"0testS1value"}

YAML stream to JSON array

If you run yaml2json on a multi-document YAML file, the output is not valid JSON. It'd be nice to have an option to output an array instead of a JSON stream.

Not a big deal, an easy workaround is to pipe it to jq -s

foo | yaml2json | jq -s -r .

ER: preserve comments using specified key

Although JSON does not support comments in the usual sense, comments in YAML could be preserved using a suitable key, which could, for example, be "//" by default.

For example, assuming "//" has been specified as the key to use for comments, the following YAML:

# comment on a
a: null

# comment on b
b: 1

# comment on c
c: 2

would become:

{
  "//": {
    "a": "comment on a",
    "b": "comment on b",
    "c": "comment on c"
  },
  "a": null,
  "b": 1,
  "c": 2
}

MacOS build is broken on High Sierra (macOS 10.13.2)

The binary image supplied with this application is broken on macOS 10.13.2

yaml2json
failed MSpanList_Insert 0x201000 0x35b4e88bf768 0x0
fatal error: MSpanList_Insert

The rebuild of image with following command fixes an issue.

go get -v github.com/bronze1man/yaml2json

1.3 panics on implicit nulls

When used with input like this:

x:
y:

The 1.3 version binary crashes with a panic, while the 1.2 version handles it correctly (outputting null as the value of the x and y keys.

Builds outdated

Looks like the builds/ directory is 4 years old. This is a useful tool, could you support or elaborate on how to build this for all those architectures now. Maybe publish builds as Github releases?

panic: resolveTable item not yet handled: N (with )

When?

Converting a large input comprising multiple documents divided with ---. No issues with the same input when using yq for example.

Source of the original file (kubernetes crds):
https://doc.crds.dev/raw/github.com/crossplane/[email protected]

What happened?

$ curl https://doc.crds.dev/raw/github.com/crossplane/[email protected] > in.yaml
$ wc -l in.yaml 
46157 in.yaml
$ yaml2json < in.yaml > out.json
panic: resolveTable item not yet handled: N (with ) [recovered]
	panic: resolveTable item not yet handled: N (with )

goroutine 1 [running]:
github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml%2ev2.handleErr(0xc000113cc8)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml.v2/yaml.go:249 +0x9a
panic(0x509cc0, 0xc000126eb0)
	/usr/local/go/src/runtime/panic.go:513 +0x1b9
github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml%2ev2.resolve(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml.v2/resolve.go:198 +0xe40
github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml%2ev2.(*decoder).scalar(0xc0003a2040, 0xc00010e070, 0x511a60, 0xc000126ea0, 0x194, 0x511a60)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml.v2/decode.go:376 +0x92
github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml%2ev2.(*decoder).unmarshal(0xc0003a2040, 0xc00010e070, 0x511a60, 0xc000126ea0, 0x194, 0x1)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml.v2/decode.go:330 +0xd6
github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml%2ev2.(*decoder).document(0xc0003a2040, 0xc00010e000, 0x511a60, 0xc000126ea0, 0x194, 0xc000113c30)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml.v2/decode.go:344 +0x7a
github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml%2ev2.(*decoder).unmarshal(0xc0003a2040, 0xc00010e000, 0x511a60, 0xc000126ea0, 0x194, 0x194)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml.v2/decode.go:320 +0x1d3
github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml%2ev2.(*Decoder).Decode(0xc000113d20, 0x503820, 0xc000126ea0, 0x0, 0x0)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/vendor/gopkg.in/yaml.v2/yaml.go:130 +0x1da
github.com/bronze1man/yaml2json/y2jLib.TranslateStream(0x554fa0, 0xc00008c000, 0x7f27482db000, 0xc00008c008, 0xc00008c008, 0x0)
	/Users/a/work/yaml2json/src/github.com/bronze1man/yaml2json/y2jLib/lib.go:16 +0x1fb
main.main()
	src/github.com/bronze1man/yaml2json/main.go:19 +0xa9

Round-Trip would be great!

Due to the lack of a tool in a comparable simplicity and power for the other way around, i.e json2yaml, I would strongly encourage you to program json2yaml tool in the same way!

YAML Specific symbols support: .inf, .nan, ~, and multi-documents

I have looked inside this repo to find if it able to convert some YAML specific symbols like .inf, .-inf, .nan, and ~.
In one of my own projects, when I have needed to same conversion from YAML to JSON it was resolved as:

  • .inf = 2147483647 (math.MaxInt32)
  • -.inf = -2147483648 (math.MinInt32)
  • .nan = 0
  • ~ = null

And what about YAML multi-document files?
It might be 2 functions where first will fetch only first document (by compatibility reasons) and processes it as JSON, and second fetches all documents and returns array of JSON converted docs.

Key named with a single letter 'n' fails the parser

I'm getting an error if there's a dictionary and a key named n.

For example:

# ERROR:
$ echo 'n: bar' | yaml2json
type mismatch: expect map key string or int; got: bool

# GOOD:
$ echo 'a: bar' | yaml2json
{"a":"bar"}

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.