3

There are many query languages for JSON, such as JMES Path and JSON Path, but none of the ones I've found interest me, e.g. JSON Path doesn't allow multiselect query (I can't return a list with different type element [car, plane, boat]) and JMES Path is really simpler compare to the JSON Path and allow the multiselect but doesn't allow the access to the parent node like .. or parent(@) or $(for the source). So I want a language which can do both, if it's possible a query language simple as JMES Path, but if there is only one and it the most difficult one it's okay I will take it !

PS: I work in javascript !

peak
  • 105,803
  • 17
  • 152
  • 177
bosskay972
  • 890
  • 1
  • 6
  • 27

2 Answers2

5

is a JSON Query language with some resemblance to JSONPath, but with the full generality of a Turing-complete programming language. jq subsumes all of JSON, and many of its constructs are JSON-like. There is no restriction on the formation of JSON arrays.

One of the ways in which problems involving "parents" and "children" can be handled in jq is using jq paths, which are nothing other than JSON arrays all of whose elements are either JSON strings (corresponding to key names) or integers (corresponding to indices into an array). Thus if $p is the path to some sub-component of a JSON document, then the path to its parent would be $p[:-1].

In practice, though, many problems involving parents and/or children can more easily be solved without using full paths. Note in particular that to_entries can be used with both JSON objects and JSON arrays.

Resources

The main website includes a tutorial, manual, and a Wiki, which has a FAQ, Cookbook, a "Language Description", and a guide for JSONPath users.

Here on stackoverflow, there are currently over 2,000 questions that have the tag. See also http://rosettacode.org/wiki/Category:Jq

peak
  • 105,803
  • 17
  • 152
  • 177
  • 1
    The jq language is based on pipes and filters, and has a full range of builtins for all the JSON types (e.g. numbers, booleans). I'd suggest taking a look at the tutorial and manual, but feel free to browse the web for introductory materials, such as https://thoughtbot.com/blog/jq-is-sed-for-json or "A Stream-Oriented Introduction to jq" [https://github.com/pkoppstein/jq/wiki/A-Stream-oriented-Introduction-to-jq] – peak Jun 04 '19 at 08:33
  • I think it will work with my actual problem but it's very complex synthax compare to JSON Path and JMES Path it will take a little bit more time to handle this language but thanks anyway, once I handle this I will destroy all my problems ! – bosskay972 Jun 04 '19 at 08:56
  • Actually the syntax is very simple, but there are some aspects of the language which may be a bit unfamiliar. Here’s one person’s summary thst I just saw: “ while this looks like an eyeful, jq actually is very easy to learn for anyone familiar with basic UNIX-like tools..” (https://www.memsql.com/blog/json-streaming-and-the-future-of-data-ingest/) – peak Jun 04 '19 at 12:05
0

Take a look at object-scan. It seems relevant to what you are asking, as it gives a lot of flexibility and access to a lot of node meta information, like parents. More examples in the readme, but here is a basic one to give you an idea

// const objectScan = require('object-scan');

const data = { a: { b: 'c' }, d: ['e', 'f'] };

const r = objectScan(['*.b', 'd[*]'], {
  filterFn: ({ value }) => ['c', 'f'].includes(value)
})(data);
console.log(r);
// => [ [ 'd', 1 ], [ 'a', 'b' ] ]
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.7.1"></script>

Disclaimer: I'm the author of object-scan

vincent
  • 1,953
  • 3
  • 18
  • 24