Awesome
jq-jsonpointer
jq module implementing JSON Pointer (RFC 6901)
This git repository contains an implementation of JSON Pointer (RFC 6901) as module for the jq data transformation language.
Table of Contents
Install
Installation requires jq version 1.5 or newer.
Put jsonpointer.jq
to a place where jq can find it as module.
One way to do so is to download the current version of the file:
mkdir -p ~/.jq && git clone https://github.com/nichtich/jq-jsonpointer.git ~/.jq/jsonpointer
Or check out this repository to directory ~/.jq/jsonpointer/
:
mkdir -p ~/.jq && wget -N https://github.com/nichtich/jsonpointer/raw/master/jsonpointer.jq
Usage
See jq manual how to use jq modules in general and API description below how to use this module.
API
pointer(json_pointer)
Returns a filter compiled from a given JSON Pointer. For instance given a JSON file input.json
:
{"foo":[{"/":42}]}
The value 42
can be accessed with JSON Pointer syntax like this:
$ jq 'include "jsonpointer"; pointer("/foo/0/~1")' input.json
42
Character -
to index the (nonexisting) member after the last array element is
not supported. If the JSON Pointer does not correspond to an existing element,
the filter returns null
instead of throwing an error.
pointer_get(tokens)
Same as pointer but expects the JSON Pointer given as array of tokens:
$ jq 'include "jsonpointer"; pointer_get(["foo","0","/"]")' input.json
42
pointer_tokens
Maps a JSON Pointer string to its tokens as array of strings.
$ jq -n 'include "jsonpointer"; "/foo/0/~1" | pointer_tokens'
[
"foo",
"0",
"/"
]
Contributing
The source code is hosted at https://github.com/nichtich/jq-jsonpointer.
Bug reports and feature requests are welcome!
License
Made available under the MIT License by Jakob Voß.