Package: ytools
Command-line tool andpythonmodule for …validatingjsonandyamlfiles against ajson-schemainjsonoryamlformatselectively dumping nodes fromyaml(orjson) documents inyamlorjsonformat.FeaturesOutputyamlasjsonorpythonOutputjsonasyamlorpython(provided that there are no
duplicate mapping entry in thejsonsource)Extract particular nodes fromyamlandjsonfiles.Ifyamlis used as output format (default) the output is a
validyamldocument.Validateyamlandjsondocuments.Thejson-schemacan be provided inyamlformat as well,
which improves readability and writability.Preserve order of mapping-keys inyamlandjsonoutput.Multi-document supportMultiple input files… as well as multipleyamldocuments within a file… and a combination of bothInstallationpip install ytoolsDescriptionFor selecting nodes,ytoolsusesjsonpath_ng.ext.The syntax is documented athttps://pypi.python.org/pypi/jsonpath-ng/1.4.2.By default (if no path is provided), complete input documents are
dumped inyamlformat (path defaults to'$').This can be used to getyamloutput forjsondocuments or vice
versa:python ytools.py input.json… for converting json to yaml, or …python ytools.py input.yaml-fjson… for the opposite direction.Additionally,yamlandjsondocuments can be validated against
ajson-schemawhich may be provided inyamlorjsonformat.schema.yamlis a sample forjson-schemainyamlformat.ytools-h$ ytools -h
Usage: /usr/local/bin/ytools [OPTION] -p JSONPATH_EXPRESSION FILE...

Dumps data from json (or yaml) documents in yaml format. Command line wrapper
for jsonpath-ng.

Options:
  -h, --help            show this help message and exit
  -p PATH, --json-path=PATH
                        Syntax for jsonpath expression:
                        https://pypi.python.org/pypi/jsonpath-ng/1.4.2
  -f OUTPUTFORMAT, --output-format=OUTPUTFORMAT
                        Output format. Can be "yaml", "json" or "python".
                        [default: yaml]
  -y YAML_OPTIONS, --yaml-options=YAML_OPTIONS
                        kwargs for yaml.dump (pyYaml) as yaml.mapping (for
                        experts). [default: '{explicit_start: True,
                        explicit_end: True, allow_unicode: True}']
  -j JSON_OPTIONS, --json-options=JSON_OPTIONS
                        kwargs for json.dumps as yaml.mapping (for experts).
                        [default: '{indent: 2, encoding: utf-8}']
  -v SCHEMA, --validate=SCHEMA
                        Validate documents against json-schema
  --encoding=ENCODING   Set encoding of input documents (if different from
                        utf-8)Command Line SamplesThe samples are based on the following data.Sample DataInput Datainput.yaml:documents:-title:Some document titlesections:-title:Some section titledescription:Some section descriptiontext:Some text for some sectionchapters:-title:Some chapter titledescription:Some chapter descriptiontext:The text of some chapter-title:Some other chapter titledescription:-descriptionparagraph1:Some description for other chapter-descriptionparagraph2:Some description for other chaptertext:The text of some other chapter-title:Some other section titledescription:Some other section descriptiontext:Some text for some other sectionchapters:-title:About encodingdescription:"SomeGerman:äöü,ÄÖÜ,ß"Schema for validatinginput.yamlschema.yaml$schema:"http://json-schema.org/schema#"definitions:chapter:type:objectproperties:title:{type:string}description:oneOf:[{type:string},{type:array}]text:{type:string}additionalProperties:falserequired:[title,description]chapters:type:arrayitems:{$ref:"#/definitions/chapter"}additionalItems:falsesection:type:objectproperties:title:{type:string}description:{type:string}text:{type:string}chapters:{$ref:"#/definitions/chapters"}additionalProperties:falserequired:[title,description]sections:type:arrayitems:{$ref:"#/definitions/section"}additionalItems:falsedocument:type:objectproperties:title:{type:string}description:{type:string}sections:{$ref:"#/definitions/sections"}additionalProperties:falserequired:[title,description]documents:type:arrayitems:{$ref:"#/definitions/document"}additionalItems:falsetype:objectproperties:documents:{$ref:"#/definitions/documents"}additionalProperties:falseOutputsWhen not providing a jsonpath expressionIf you don’t provide a jsonpath expression using the-poption
ytools uses'$'as default and therefore dumps the complete input:🎼  ytools input.yaml
---
documents:
- title: Some document title
  description: The document's description
  sections:
  - title: Some section title
    description: Some section description
    text: Some text for some section
    chapters:
    - {title: Some chapter title, description: Some chapter description, text: The
        text of some chapter}
    - title: Some other chapter title
      description:
      - {descriptionparagraph1: Some description for other chapter}
      - {descriptionparagraph2: Some description for other chapter}
      text: The text of some other chapter
  - title: Some other section title
    description: Some other section description
    text: Some text for some other section
    chapters:
    - {title: About encoding, description: 'Some German: äöü,ÄÖÜ,ß'}
...Output formatyaml(default)With theyamloutput format by default, each match is output as a
separateyamldocument. This way we achieve that the output is validyaml.:🎼  ytools -p '$..chapters[*].description' input.yaml
--- Some chapter description
...

---
- {descriptionparagraph1: Some description for other chapter}
- {descriptionparagraph2: Some description for other chapter}
...

--- 'Some German: äöü,ÄÖÜ,ß'
...If you want different behavior you can setexplicit_startand/orexplicit_endtoFalse. In this case the output will no longer be
guaranteed to be validyaml:🎼  ytools -p '$..chapters[*].description' input.yaml --yaml-options='{explicit_start: False, explicit_end: False}'
Some chapter description
...

- {descriptionparagraph1: Some description for other chapter}
- {descriptionparagraph2: Some description for other chapter}

'Some German: äöü,ÄÖÜ,ß'Other output formatsUnfortunately, when using thejsonorpythonoutput format the
same selection can’t produce validjsonorpythonoutput. That’s
because neither json nor python support the concept of (multiple)
documents:🎼  ytools -p '$..chapters[*].description' input.yaml -f json --json-options='{indent: 4}'
"Some chapter description"
[
    {
        "descriptionparagraph1": "Some description for other chapter"
    },
    {
        "descriptionparagraph2": "Some description for other chapter"
    }
]
"Some German: \u00e4\u00f6\u00fc,\u00c4\u00d6Y,\u00df"That’s definitely not valid json.Neither is the following valid python:🎼  ytools -p '$..chapters[*].description' input.yaml -f python
Some chapter description
[{'descriptionparagraph1': 'Some description for other chapter'}, {'descriptionparagraph2': 'Some description for other chapter'}]
Some German: äöü,ÄÖÜ,ßSoif you ever want to process the output automatically please stick
to ``yaml``.Python SamplesSample DataInput Datatest/sampledata.yamlname:'my_name'date:'2017-10-01T10:55:00Z'metrics:percentage:value:87trend:stableSchema for Validatingtest/sampleschema.yamltype:objectproperties:name:{type:string}date:{type:string,format:date-time}metrics:type:objectproperties:percentage:type:objectproperties:value:type:numberminimum:0maximum:100trend:{type:string,enum:[down,stable,up]}additionalProperties:falserequired:[value,trend]additionalProperties:falserequired:[percentage]additionalProperties:falserequired:[name,date,metrics]Validationytools.validate("test/sampleschema.yaml",["test/sampledata.yaml"])… will not output anything because of successful validation. Play
around if you want to see failing validation - it’s quite easy to make
it fail ;-)Dumpingytools.dump("test/sampledata.yaml","$.metrics",yaml_options="default_flow_style: false")… will output …---
percentage:
  value: 87
  trend: stable
...