项目作者: openuado

项目描述 :
Parse/Read yaml or json files directly in your shell (sh, bash, ksh, ...)
高级语言: Python
项目地址: git://github.com/openuado/niet.git
创建时间: 2018-01-30T15:16:25Z
项目社区:https://github.com/openuado/niet

开源协议:MIT License

下载


niet

Build
PyPI
PyPI - Python Version
PyPI - Status
Downloads
Downloads

Get data from YAML, JSON, and TOML file directly in your shell.


How to easily parse and retrieve data from YAML file in our shell?

The previous question, few years ago, led us to the development of niet.

Indeed, at that time, we needed a way to store and retrieve data for our own
needs. We created niet to read those data. The goal was to develop a tools
that will allow us to standardize how we parse YAML locally or in our CI
pipelines. We wanted something reusable and easily distribuable. Niet was born.

Over the years niet evolved to introduce the support of other formats like
TOML.

Niet is like xmllint or
jq but for YAML, JSON and TOML data -
you can use it to slice and filter and map and transform structured data.

You can easily retrieve data by using simple expressions or using
xpath advanced features to access non-trivial data.

You can easily convert YAML format into JSON, or TOML formats and vice versa.

Niet is writen in Python so you can install it from a package manager (from
PyPi) or directly by cloning this repository - no specific system rights are
needed to install it.

Main Features

  • Extract elements by using xpath syntax
  • Extract values from JSON, YAML, and TOML format
  • Automaticaly detect format (json/yaml)
  • Read data from a web resource
  • Read data from file or pass data from stdin
  • Format output values
  • Format output to be reused by shell eval
  • Convert YAML to JSON, or TOML and vice versa

Install or Update niet

  1. $ pip install -U niet

Requirements

  • Python 3.9 or higher

Supported versions

Since niet 2.0 the support of python 2.7 have been dropped so if
if you only have python 2.7 at hands then you can use previous version (lower
to 2.0) but you should consider first that no support will be given on
these versions (no bugfix, no new feature, etc). If you report an issue or
or propose a new feature then they will be addressed only for current or
higher version.

Usage

Help and options

  1. $ niet --help
  2. usage: niet [-h] [-a ADDITIONAL_OBJECTS [ADDITIONAL_OBJECTS ...]] [-f {json,yaml,toml,eval,newline,ifs,squote,dquote,comma}] [-i] [-o OUTPUT_FILE] [-s] [-v] [--debug] object [file]
  3. Read data from YAML or JSON file
  4. positional arguments:
  5. object Path to object. Based on jsmespath identifiers (https://jmespath.org/specification.html#identifiers) Use '.' to get whole file. (eg: a.b.c)
  6. file Optional JSON or YAML local filename or distant web resource at raw format. If not provided niet read from stdin
  7. options:
  8. -h, --help show this help message and exit
  9. -a ADDITIONAL_OBJECTS [ADDITIONAL_OBJECTS ...], --additional-objects ADDITIONAL_OBJECTS [ADDITIONAL_OBJECTS ...]
  10. Path to additional objects to search. Here you can pass a list of additional researchs. Allow you to combine researchs into the same command call. The researchs will be made on the original file as with the
  11. `object` parameter. Niet will output the results sequentially without delimiter between the results. If the `--output` argument is given by user, the results are appended at the end of the file sequentially. Based
  12. on jsmespath identifiers (https://jmespath.org/specification.html#identifiers) Use '.' to get whole file. (eg: a.b.c)
  13. -f {json,yaml,toml,eval,newline,ifs,squote,dquote,comma}, --format {json,yaml,toml,eval,newline,ifs,squote,dquote,comma}
  14. output format
  15. -i, --in-place Perform modification in place. Will so alter read file
  16. -o OUTPUT_FILE, --output OUTPUT_FILE
  17. Print output in a file instead of stdout (surcharged by in-place parameter if set)
  18. -s, --silent silent mode, doesn't display message when element was not found
  19. -v, --version print the Niet version number and exit (also --version)
  20. --debug Activate the debug mode (based on pdb)
  21. output formats:
  22. json Return object in JSON
  23. yaml Return object in YAML
  24. toml Return object in TOML
  25. eval Return result in a string evaluable by a shell eval command as an input
  26. newline Return all elements of a list in a new line
  27. ifs Return all elements of a list separated by IFS env var
  28. squote Add single quotes to result
  29. dquote Add double quotes to result
  30. comma Return all elements separated by commas

With Json from stdin

  1. $ echo '{"foo": "bar", "fizz": {"buzz": ["1", "2", "Fizz", "4", "Buzz"]}}' | niet fizz.buzz
  2. 1
  3. 2
  4. Fizz
  5. 4
  6. Buzz
  7. $ echo '{"foo": "bar", "fizz": {"buzz": ["1", "2", "Fizz", "4", "Buzz"]}}' | niet fizz.buzz -f squote
  8. '1' '2''Fizz' '4' 'Buzz'
  9. $ echo '{"foo": "bar", "fizz": {"buzz": ["1", "2", "fizz", "4", "buzz"]}}' | niet . -f yaml
  10. fizz:
  11. buzz:
  12. - '1'
  13. - '2'
  14. - fizz
  15. - '4'
  16. - buzz
  17. foo: bar
  18. $ echo '{"foo": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet "fizz.buzz[2]"
  19. two
  20. $ echo '{"foo": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f dquote "fizz.buzz[0:2]"
  21. "zero" "one"
  22. $ echo '{"foo": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f dquote "fizz.buzz[:3]"
  23. "zero" "one" "two"

With YAML file

Consider the yaml file with the following content:

  1. # /path/to/your/file.yaml
  2. project:
  3. meta:
  4. name: my-project
  5. foo: bar
  6. list:
  7. - item1
  8. - item2
  9. - item3
  10. test-dash: value

You can download the previous example locally for testing purpose or use the command line for this:

  1. wget https://gist.githubusercontent.com/4383/53e1599663b369f499aa28e27009f2cd/raw/389b82c19499b8cb84a464784e9c79aa25d3a9d3/file.yaml

You can retrieve data from this file by using niet like this:

  1. $ niet ".project.meta.name" /path/to/your/file.yaml
  2. my-project
  3. $ niet ".project.foo" /path/to/your/file.yaml
  4. bar
  5. $ niet ".project.list" /path/to/your/file.yaml
  6. item1 item2 item3
  7. $ # assign return value to shell variable
  8. $ NAME=$(niet ".project.meta.name" /path/to/your/file.yaml)
  9. $ echo $NAME
  10. my-project
  11. $ niet project.'"test-dash"' /path/to/your/file.json
  12. value

With JSON file

Consider the json file with the following content:

  1. {
  2. "project": {
  3. "meta": {
  4. "name": "my-project"
  5. },
  6. "foo": "bar",
  7. "list": [
  8. "item1",
  9. "item2",
  10. "item3"
  11. ],
  12. "test-dash": "value"
  13. }
  14. }

You can download the previous example locally for testing purpose or use the command line for this:

  1. wget https://gist.githubusercontent.com/4383/1bab8973474625de738f5f6471894322/raw/0048cd2310df2d98bf4f230ffe20da8fa615cef3/file.json

You can retrieve data from this file by using niet like this:

  1. $ niet "project.meta.name" /path/to/your/file.json
  2. my-project
  3. $ niet "project.foo" /path/to/your/file.json
  4. bar
  5. $ niet "project.list" /path/to/your/file.json
  6. item1 item2 item3
  7. $ # assign return value to shell variable
  8. $ NAME=$(niet "project.meta.name" /path/to/your/file.json)
  9. $ echo $NAME
  10. my-project
  11. $ niet project.'"test-dash"' /path/to/your/file.json
  12. value

Object Identifiers

An identifier is the most basic expression and can be used to extract a single
element from a JSON/YAML document. The return value for an identifier is
the value associated with the identifier. If the identifier does not
exist in the JSON/YAML document, than niet display a specific message and
return the error code 1, example:

  1. $ echo '{"foo": "bar", "fizz": {"buzz": ["1", "2", "3"]}}' | niet fizz.gogo
  2. Element not found: fizz.gogo
  3. $ echo $?
  4. 1

See the related section for more info on how to manage
errors with niet.

Niet is based on jmespath to find results so for complexe research you can
refer to the jmespath specifications
to use identifiers properly.

If you try to search for an identifier who use some dash you need to surround
your research expression with simple and double quotes, examples:

  1. $ echo '{"foo-biz": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f dquote '"foo-biz"'
  2. bar
  3. $ echo '{"key-test": "value"}' | niet '"key-test"'
  4. value

However, niet will detect related issues and surround automatically your
identifier if jmespath fail to handle it.

Hence, the following examples will return similar results than the previous
examples:

  1. $ echo '{"foo-biz": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f dquote foo-biz
  2. bar
  3. $ echo '{"key-test": "value"}' | niet key-test
  4. value

If your object is not at the root of your path, an example is available in
tests/sample/sample.json, then you need to only surround the researched
identifier like this project.'"test-dash"'

  1. {
  2. "project": {
  3. "meta": {
  4. "name": "my-project"
  5. },
  6. "foo": "bar",
  7. "list": [
  8. "item1",
  9. "item2",
  10. "item3"
  11. ],
  12. "test-dash": "value"
  13. }
  14. }

Example:

  1. niet project.'"test-dash"' tests/sample/sample.json

Further examples with jmespath identifiers.

Additional objects

Additional objects allow you to combine more than one query.
The --additional-objects parameter accept a list of objects strings.
These objects are the same thing that the base object used to query
your inputs.

This parameter allow to generate advanced output from your input, let see an
example:

Consider the following yaml example:

  1. configuration:
  2. warehouse: warehouse-name
  3. database: database-name
  4. object_type:
  5. schema:
  6. schema1: "/path/to/schema1.sql"
  7. schema2: "/path/to/schema2.sql"

The following command will allow us to generate an output constitued from the
results of the two objects used as query:

  1. $ niet ".configuration.object_type | keys(@)[0]" config.yaml
  2. -a ".configuration.object_type.schema.[keys(@)[0], values(@)[0]]"

The previous command will output:

  1. schema
  2. schema1
  3. /path/to/schema1.sql

This output wouldn’t be possible without combining the result of two queries,
the additional objects are made for that.

Outputs of these additional objects are printed sequentially in the order they
are given in the command line.

Output

Stdout

By default, niet print the output on stdout.

Save output to a file

It if possible to pass a filename using -o or —output argument to writes
directly in a file. This file will be created if not exists or will be
replaced if already exists.

In-file modification

It is possible to modify directly a file using -i or —in-place argument. This will replace
the input file by the output of niet command. This can be used to extract some data of a file or
reindent a file.

Output formats

You can change the output format using the -f or —format optional
argument.

By default, niet detect the input format and display complex objects
in the same format. If the object is a list or a value, newline output
format will be used.

Output formats are:

  • ifs
  • squote
  • dquote
  • newline
  • yaml
  • json
  • toml

ifs

Ifs output format print all values of a list or a single value in one line.
All values are separated by the content of IFS environment variable if defined,
space otherwise.

Examples (consider the previous YAML file example):

  1. $ IFS="|" niet .project.list /path/to/your/file.yaml -f ifs
  2. item1|item2|item3
  3. $ IFS=" " niet .project.list /path/to/your/file.yaml -f ifs
  4. item1 item2 item3
  5. $ IFS="@" niet .project.list /path/to/your/file.yaml -f ifs
  6. item1@item2@item3

This is usefull in a shell for loop,
but your content must, of course, don’t contain IFS value:

  1. OIFS="$IFS"
  2. IFS="|"
  3. for i in $(niet .project.list /path/to/your/file.yaml -f ifs); do
  4. echo ${i}
  5. done
  6. IFS="${OIFS}"

Previous example provide the following output:

  1. item1
  2. item2
  3. item3

For single quoted see squote ouput or dquote double quoted output with IFS

squote

Squotes output format print all values of a list or a single value in one line.
All values are quoted with single quotes and are separated by IFS value.

Examples (consider the previous YAML file example):

  1. $ # With the default IFS
  2. $ niet .project.list /path/to/your/file.yaml -f squote
  3. 'item1' 'item2' 'item3'
  4. $ # With a specified IFS
  5. $ IFS="|" niet .project.list /path/to/your/file.yaml -f squote
  6. 'item1'|'item2'|'item3'

dquote

Dquotes output format print all values of a list or a single value in one line.
All values are quoted with a double quotes and are separated by IFS value.

Examples (consider the previous YAML file example):

  1. $ # With the default IFS
  2. $ niet .project.list /path/to/your/file.yaml -f dquote
  3. 'item1' 'item2' 'item3'
  4. $ # With a specified IFS
  5. $ IFS="|" niet .project.list /path/to/your/file.yaml -f dquote
  6. "item1"|"item2"|"item3"

newline

newline output format print one value of a list or a single value per line.

The newline format is mostly usefull with shell while read loops and
with script interactions.

Example:

  1. while read value: do
  2. echo $value
  3. done < $(niet --format newline project.list your-file.json)

comma

comma output format print results on the same line and separated by commas.

The comma format allow you to format your outputs to consume your results
with other commands lines interfaces. By example some argument parser
allow you to pass multi values for the same parameter (the
beagle command per
example allow you to
repeat the --repo option).

Example of integration with beagle and shell:

  1. $ OSLO_PROJECTS_URL=https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml
  2. $ beagle search \
  3. -f link \
  4. --repo $(niet "oslo.deliverables.*.repos[0]" ${OSLO_PROJECTS_URL} -f comma) 'venv'

The previous command will return all the links of files
who contains venv on the openstack oslo’s scope of projects (pbr,
taskflow, oslo.messaging, etc).

Else another with a more reduced scope on openstack oslo’s projects:

  1. $ niet "oslo.deliverables.*.repos[0][?contains(@, \`oslo\`) == \`true\`]" \
  2. https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml \
  3. -f comma
  4. openstack/oslo-cookiecutter,openstack/oslo-specs,openstack/oslo.cache,
  5. openstack/oslo.concurrency,openstack/oslo.config,openstack/oslo.context,
  6. openstack/oslo.db,openstack/oslo.i18n,openstack/oslo.limit,openstack/oslo.log,
  7. openstack/oslo.messaging,openstack/oslo.middleware,
  8. openstack/oslo.policy,openstack/oslo.privsep,openstack/oslo.reports,
  9. openstack/oslo.rootwrap,openstack/oslo.serialization,openstack/oslo.service,
  10. openstack/oslo.tools,openstack/oslo.upgradecheck,openstack/oslo.utils,
  11. openstack/oslo.versionedobjects,openstack/oslo.vmware,openstack/oslotest

In the previous example we retrieve only the projects repos who contains
oslo in their names, so other projects like taskflow, pbr, etc will
be ignored.

eval

Eval output format allow you to eval output string to initialize shell
variable generated from your JSON/YAML content.

You can intialize shell variables from your entire content, example:

  1. $ echo '{"foo-biz": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f eval .
  2. foo_biz="bar";fizz__buzz=( zero one two three )
  3. $ eval $(echo '{"foo-biz": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f eval .)
  4. $ echo ${foo_biz}
  5. bar
  6. $ echo ${fizz__buzz}
  7. zero one two three
  8. $ eval $(echo '{"foo-biz": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f eval '"foo-biz"'); echo ${foo_biz}
  9. bar
  10. $ echo '{"foo-biz": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f eval fizz.buzz
  11. fizz_buzz=( zero one two three );

Parent elements are separated by __ by example the fizz.buzz element
will be represented by a variable named fizz__buzz. You need to consider
that when you call your expected variables.

Also you can initialize some shell array from your content and loop over in
a shell maner:

  1. $ eval $(echo '{"foo-biz": "bar", "fizz": {"buzz": ["zero", "one", "two", "three"]}}' | niet -f eval fizz.buzz)
  2. $ for el in ${fizz_buzz}; do echo $el; done
  3. zero
  4. one
  5. two
  6. three

yaml

YAML output format force output to be in YAML regardless the input file format.

json

JSON output format force output to be in JSON regardless the input file format.

toml

TOML output format force output to be in TOML regardless the input file format.

Read data from a web resource

Niet allow you to read data (json/yaml/toml) from a web resource accessible by
using the HTTP protocole (introduced in niet 2.1).

This can be done by passing an url to niet which refer to a raw content (json,
yaml, or toml).

Here is some examples with the openstack governance’s projects data:

  1. $ # List all the oslo projects repos (https://wiki.openstack.org/wiki/Oslo)
  2. $ niet "oslo.deliverables.*.repos[0]" \
  3. https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml
  4. openstack/automaton
  5. openstack/castellan
  6. ...
  7. openstack/debtcollector
  8. ...
  9. openstack/futurist
  10. openstack/oslo.cache
  11. openstack/oslo.concurrency
  12. openstack/oslo.config
  13. openstack/oslo.context
  14. openstack/oslo.db
  15. openstack/oslo.i18n
  16. openstack/oslo.limit
  17. openstack/oslo.log
  18. openstack/oslo.messaging
  19. openstack/oslo.middleware
  20. openstack/oslo.policy
  21. ...
  22. openstack/oslo.service
  23. openstack/osprofiler
  24. openstack/pbr
  25. ...
  26. openstack/stevedore
  27. openstack/taskflow
  28. openstack/tooz
  29. openstack/whereto
  30. $ niet oslo.service \
  31. https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml
  32. Common libraries
  33. $ # Get the openstack oslo's mission
  34. $ niet oslo.mission \
  35. https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml
  36. To produce a set of python libraries containing code shared by OpenStack projects.
  37. The APIs provided by these libraries should be high quality, stable, consistent,
  38. documented and generally applicable.
  39. $ eval $(niet oslo.service \
  40. https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml -f eval) && \
  41. test "${oslo_service}" = "Common libraries"
  42. $ # Get the name of the oslo PTL
  43. $ eval $(niet oslo.ptl.name \
  44. https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml -f eval)
  45. $ echo "${oslo_ptl_name}" # now display your evaluated result
  46. $ # Convert original distant yaml file into json
  47. $ niet . https://raw.githubusercontent.com/openstack/governance/master/reference/projects.yaml -f json

For further examples of filters and selections please take a look to
the jmespath’s doc.

Result not found

By default when no results was found niet display a specific message and return
the error code 1, example:

  1. $ echo '{"foo": "bar", "fizz": {"buzz": ["1", "2", "3"]}}' | niet fizz.gogo
  2. Element not found: fizz.gogo
  3. $ echo $?
  4. 1

You can avoid this behavior by passing niet into a silent mode.

Silent mode allow you to hide the specific message error but continue to return
a status code equal to 1 when the key was not found.

You can use the silent mode by using the flag -s/--silent, example:

  1. $ echo '{"foo": "bar", "fizz": {"buzz": ["1", "2", "3"]}}' | niet fizz.gogo -s
  2. $ echo $?
  3. 1

Deal with errors

When your JSON file content are not valid niet display an error and exit
with return code 1

You can easily protect your script like this:

  1. PROJECT_NAME=$(niet project.meta.name your-file.yaml)
  2. if [ "$?" = "1" ]; then
  3. echo "Error occur ${PROJECT_NAME}"
  4. else
  5. echo "Project name: ${PROJECT_NAME}"
  6. fi

Examples

You can try niet by using the samples provided with the project sources code.

All the following examples use the sample file available in niet sources code
at the following location tests/samples/sample.yaml.

Sample example:

  1. # tests/samples/sample.yaml
  2. project:
  3. meta:
  4. name: my-project
  5. foo: bar
  6. list:
  7. - item1
  8. - item2
  9. - item3

Extract a single value

Retrieve the project name:

  1. $ niet project.meta.name tests/samples/sample.yaml
  2. my-project

Consider the following content:

  1. $ cat /var/lib/libvirt/dnsmasq/virbr0.status
  2. [
  3. {
  4. "ip-address": "192.168.122.113",
  5. "mac-address": "52:54:00:91:14:02",
  6. "hostname": "rhel79",
  7. "expiry-time": 1644251254
  8. },
  9. {
  10. "ip-address": "192.168.122.162",
  11. "mac-address": "52:54:00:23:37:ed",
  12. "hostname": "satellite",
  13. "expiry-time": 1644251837
  14. }
  15. ]

Here we want to retrieve the value of the ip-address field when the hostname
is equal to satellite. The following command will allow you to get this
value:

  1. $ sed 's/ip/_/g' /var/lib/libvirt/dnsmasq/virbr0.status | niet "[?hostname=='satellite'].ip"
  2. 192.168.122.162

You should notice that first we replace - by _ by using the sed
command. We do that because jmespath, the underlying library used by niet
, poorly handle key that contain -. We chosen to replace all - by _ to avoid
any issues elsewhere on the file

Here is an exemple of an automated ssh connection in a kvm virtualised lab
environment by looking for vmname in dhcp file with niet and performing the
ssh connection to the server even if its ip changed.

The ssh connection here can be performed with this command:

  1. ssh -o ProxyCommand='nc $(sed 's/-/_/g' /var/lib/libvirt/dnsmasq/virbr0.status | niet "[?hostname=='''%h'''].ip_address") %p' root@rhel79

Tips - to ease that use you can for example set this .ssh/config entry:

  1. host lab-*
  2. user root
  3. ProxyCommand /usr/bin/nc $(sed 's/-/_/g' /var/lib/libvirt/dnsmasq/virbr0.status | niet "[?hostname=='$(echo %h | cut -d'-' -f2 )'].ip_address") %p

And then perform a ssh lab-rhel79 or a ssh lab-satellite to join all VMs
from your lab, by the hostname prefixed by lab-.

Extract a list and parse it in shell

Deal with list of items

  1. $ for el in $(niet project.list tests/samples/sample.yaml); do echo ${el}; done
  2. item1
  3. item2
  4. item3

Also you can eval your niet output to setput some shell variables
that you can reuse in your shell scripts, the following example is similar to
the previous example but make use of the eval ouput format (-f eval):

  1. $ eval $(niet -f eval project.list tests/samples/sample.yaml)
  2. $ for el in ${project__list}; do echo $el; done
  3. zero
  4. one
  5. two
  6. three

Extract a complex object and parse it in shell

Extract the object as JSON to store it in shell variable :

  1. $ project="$(niet -f json .project tests/samples/sample.yaml)"

Then parse it after in bash in this example:

  1. $ niet .meta.name <<< $project
  2. my-project

Transform JSON into YAML

With niet you can easily convert your JSON into YAML

  1. $ niet . tests/samples/sample.json -f yaml
  2. project:
  3. foo: bar
  4. list:
  5. - item1
  6. - item2
  7. - item3
  8. meta:
  9. name: my-project

Transform YAML into JSON

With niet you can easily convert your YAML into JSON

  1. $ niet . tests/samples/sample.yaml -f json
  2. {
  3. "project": {
  4. "meta": {
  5. "name": "my-project"
  6. },
  7. "foo": "bar",
  8. "list": [
  9. "item1",
  10. "item2",
  11. "item3"
  12. ]
  13. }
  14. }

Transform JSON into TOML

With niet you can easily convert your JSON into TOML

  1. $ niet . tests/samples/sample.json -f toml
  2. [project]
  3. foo = "bar"
  4. list = ["item1", "item2", "item3"]
  5. test-dash = "value"
  6. [project.meta]
  7. name = "my-project"

Transform YAML into TOML

With niet you can easily convert your YAML into TOML

  1. $ niet . tests/samples/sample.yaml -f toml
  2. [project]
  3. foo = "bar"
  4. list = ["item1", "item2", "item3"]
  5. test-dash = "value"
  6. [project.meta]
  7. name = "my-project"

Transform TOML into YAML

With niet you can easily convert your TOML into YAML

  1. niet . tests/samples/sample.toml -f yaml
  2. project:
  3. foo: bar
  4. list:
  5. - item1
  6. - item2
  7. - item3
  8. meta:
  9. name: my-project
  10. test-dash: value

Indent JSON file

This is an example of how to indent a JSON file :

  1. $ niet . tests/samples/sample_not_indented.json
  2. {
  3. "project": {
  4. "meta": {
  5. "name": "my-project"
  6. },
  7. "foo": "bar",
  8. "list": [
  9. "item1",
  10. "item2",
  11. "item3"
  12. ],
  13. "test-dash": "value"
  14. }
  15. }

Handle keys that contains dots

You may want to retrieve values from keys that contains dots, example:

  1. .foo:
  2. something: "a"
  3. bar:
  4. something: "b"
  5. foo.z:
  6. something: "c"

Then you must surround keys that contains dots with quotes, example:

  1. $ niet '".foo"' /tmp/test.yaml
  2. something: a
  3. $ niet '"foo.z"' /tmp/test.yaml
  4. something: c

Tips

You can pass your search with or without quotes like this:

  1. $ niet project.meta.name your-file.yaml
  2. $ niet "project.meta.name" your-file.yaml

You can execute niet step by step by using the debug mode. It will allow
you to inspect your execution during your debug sessions.

Contribute

If you want to contribute to niet please first read the contribution guidelines

Licence

This project is under the MIT License.

See the license file for more details