Code Monkey home page Code Monkey logo

y2j's Introduction

#NAME y2j, j2y, yq - filters to convert JSON to YAML, YAML to JSON and YAML to YAML.

#SYNOPSIS # convert from YAML to JSON y2j < yaml > json j2y -d < yaml > json

# convert from YAML to JSON with optional trailing jq transformation
y2j {jq-filter} < yaml

# convert from JSON to YAML
j2y < json > yaml
y2j -d < json > yaml

# convert from JSON to YAM with optional leading jq transformation
j2y {jq-filter} < json > yaml

# convert YAML to JSON, run jq, convert back to YAML
yq {jq-filter} < yaml > yaml

# create an installer that will install y2j.sh into /usr/local/bin, then run that script with bash
y2j.sh installer /usr/local/bin | sudo bash

#DESCRIPTION

This package provides filters for transforming JSON to YAML, YAML to JSON and YAML to YAML.

YAML to YAML transformations are performed by applying a jq filter to a JSON transformation of the YAML input stream with y2j and transforming the resulting JSON stream back to YAML with j2y.

The script will use the local instances of jq, python and the required python modules if they exist locally or will use a docker container based on the wildducktheories/y2j image otherwise.

#INSTALLATION

docker run --rm wildducktheories/y2j y2j.sh installer /usr/local/bin | sudo bash

Replace /usr/local/bin with a different directory to specify a different installation location or omit to default to /usr/local/bin.

#EXAMPLES ##j2y

echo '{ "foo": [ { "id": 1 } , {"id": 2 }] }' | j2y

yields:

foo:
- id: 1
- id: 2

##j2y - with jq pre-stage

echo '{"foo": "bar"}{"foo": "baz"}' | j2y -s .

yields:

- foo: bar
- foo: baz

##y2j

(
	y2j <<EOF
foo:
- id: 1
- id: 2
EOF
) | jq -c .

yields:

{"foo":[{"id":1},{"id":2}]}

##yq

yq '.foo=(.foo[]|select(.id == 1)|[.])' <<EOF
foo:
- id: 1
- id: 2
EOF

yields:

foo:
- id: 1

#LIMITATIONS

  • y2j and yq only support the subset of YAML streams that can be losslessly represented in JSON - that is: trees. Graphs, anchors and references are not supported.
  • j2y only supports reading of a single JSON object or a single JSON array from stdin. If the JSON input contains multiple objects, consider using '-s .' with j2y to slurp the input into a single JSON array.
  • yq only supports jq-filters that are guaranteed to produce a single JSON object or array.

Behaviour with inputs or filters that do not satisfy these constraints is not defined.

#AUTHOR

Jon Seymour <[email protected]>

#ACKNOWLEDGMENTS

Conversions used by y2j.sh are based on the commandlinefu scripts found here:

Filtering is implemented with jq. See http://stedolan.github.io/jq/.

#REVISIONS

##1.1

  • Added support for yq.

##1.0

  • Initial release.

y2j's People

Contributors

jonseymour avatar

Watchers

Henrik Holmboe avatar James Cloos avatar

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.