JSON 有查询语言吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/777455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a query language for JSON?
提问by allclaws
Is there a (roughly) SQL or XQuery-like language for querying JSON?
是否有(大致)SQL 或类似 XQuery 的语言来查询 JSON?
I'm thinking of very small datasets that map nicely to JSON where it would be nice to easily answer queries such as "what are all the values of X where Y > 3" or to do the usual SUM / COUNT type operations.
我正在考虑非常小的数据集,它们可以很好地映射到 JSON,在那里很容易回答诸如“X 的所有值是什么,其中 Y > 3”或执行通常的 SUM / COUNT 类型操作。
As completely made-up example, something like this:
作为完全虚构的示例,如下所示:
[{"x": 2, "y": 0}}, {"x": 3, "y": 1}, {"x": 4, "y": 1}]
SUM(X) WHERE Y > 0 (would equate to 7)
LIST(X) WHERE Y > 0 (would equate to [3,4])
I'm thinking this would work both client-side and server-side with results being converted to the appropriate language-specific data structure (or perhaps kept as JSON)
我认为这将在客户端和服务器端都有效,结果被转换为适当的特定于语言的数据结构(或者可能保留为 JSON)
A quick Googling suggests that people have thought about it and implemented a few things (JAQL), but it doesn't seem like a standard usage or set of libraries has emerged yet. While each function is fairly trivial to implement on its own, if someone has already done it right I don't want to re-invent the wheel.
一个快速的谷歌搜索表明人们已经考虑过它并实现了一些东西(JAQL),但它似乎还没有出现标准用法或一组库。虽然每个功能单独实现都相当简单,但如果有人已经做对了,我不想重新发明轮子。
Any suggestions?
有什么建议?
Edit: This may indeed be a bad idea or JSON may be too generic a format for what I'm thinking.. The reason for wanting a query language instead of just doing the summing/etc functions directly as needed is that I hope to build the queries dynamically based on user-input. Kinda like the argument that "we don't need SQL, we can just write the functions we need". Eventually that either gets out of hand or you end up writing your own version of SQL as you push it further and further. (Okay, I know that is a bit of a silly argument, but you get the idea..)
编辑:这可能确实是一个坏主意,或者 JSON 可能是我所想的格式过于通用.. 想要查询语言而不是根据需要直接执行求和/等函数的原因是我希望构建根据用户输入动态查询。有点像“我们不需要 SQL,我们可以只编写我们需要的函数”的论点。最终,这要么失控,要么随着你越来越深入地编写自己的 SQL 版本。(好吧,我知道这有点愚蠢,但你明白了..)
采纳答案by StaxMan
Sure, how about:
当然,怎么样:
They all seem to be a bit work in progress, but work to some degree. They are also similar to XPath and XQuery conceptually; even though XML and JSON have different conceptual models (hierarchic vs object/struct).
它们似乎都在进行中,但在某种程度上是有效的。它们在概念上也类似于 XPath 和 XQuery;即使 XML 和 JSON 具有不同的概念模型(分层 vs 对象/结构)。
EDITSep-2015: Actually there is now JSON Pointerstandard that allows very simple and efficient traversal of JSON content. It is not only formally specified, but also supported by many JSON libraries. So I would call it actual real useful standard, although due to its limited expressiveness it may or may not be considered Query Language per se.
编辑2015 年 9 月:实际上现在有JSON 指针标准,允许非常简单有效地遍历 JSON 内容。它不仅被正式指定,而且被许多 JSON 库支持。所以我会称它为真正有用的标准,尽管由于它的表达能力有限,它本身可能会也可能不会被视为查询语言。
回答by Hugoware
I'd recommend my project I'm working on called jLinq. I'm looking for feedback so I'd be interested in hearing what you think.
我会推荐我正在从事的名为 jLinq 的项目。我正在寻找反馈,所以我很想听听你的想法。
If lets you write queries similar to how you would in LINQ...
如果让您编写类似于在 LINQ 中的查询方式...
var results = jLinq.from(records.users)
//you can join records
.join(records.locations, "location", "locationId", "id")
//write queries on the data
.startsWith("firstname", "j")
.or("k") //automatically remembers field and command names
//even query joined items
.equals("location.state", "TX")
//and even do custom selections
.select(function(rec) {
return {
fullname : rec.firstname + " " + rec.lastname,
city : rec.location.city,
ageInTenYears : (rec.age + 10)
};
});
It's fully extensible too!
它也是完全可扩展的!
The documentation is still in progress, but you can still try it online.
文档仍在进行中,但您仍然可以在线试用。
回答by Jonathan
Update: XQuery 3.1can query either XML or JSON - or both together. And XPath 3.1can too.
更新:XQuery 3.1可以查询 XML 或 JSON - 或同时查询。和XPath的3.1也可以。
The list is growing:
名单正在增加:
回答by xor
jmespath works really quite easy and well, http://jmespath.org/It is being used by Amazon in the AWS command line interface, so it′s got to be quite stable.
jmespath 工作起来非常简单而且很好,http://jmespath.org/ 亚马逊在 AWS 命令行界面中使用它,所以它必须非常稳定。
回答by peak
jqis a JSON query language, mainly intended for the command-line but with bindings to a wide range of programming languages (Java, node.js, php, ...) and even available in the browser via jq-web.
JQ是ĴSON query语言,主要用于命令行但绑定到一个广泛的编程语言(Java,Node.js的,PHP,...),甚至在通过浏览器提供的JQ-网络。
Here are some illustrations based on the original question, which gave this JSON as an example:
以下是基于原始问题的一些插图,以这个 JSON 为例:
[{"x": 2, "y": 0}}, {"x": 3, "y": 1}, {"x": 4, "y": 1}]
SUM(X) WHERE Y > 0 (would equate to 7)
SUM(X) 其中 Y > 0(等于 7)
map(select(.y > 0)) | add
LIST(X) WHERE Y > 0 (would equate to [3,4])
LIST(X) WHERE Y > 0(等于 [3,4])
map(.y > 0)
jq syntax extends JSON syntax
jq 语法扩展了 JSON 语法
Every JSON expression is a valid jq expression, and expressions such as [1, (1+1)]and {"a": (1+1)}` illustrate how jq extends JSON syntax.
每个 JSON 表达式都是一个有效的 jq 表达式,诸如[1, (1+1)]和 {"a": (1+1)}`这样的表达式说明了 jq 如何扩展 JSON 语法。
A more useful example is the jq expression:
一个更有用的例子是 jq 表达式:
{a,b}
which, given the JSON value {"a":1, "b":2, "c": 3}, evaluates to {"a":1, "b":2}.
其中,给定 JSON 值{"a":1, "b":2, "c": 3},计算结果为{"a":1, "b":2}。
回答by Roger
The built-in array.filter()methodmakes most of these so-called javascript query libraries obsolete
内置array.filter()方法使大多数这些所谓的 javascript 查询库过时了
You can put as many conditions inside the delegate as you can imagine: simple comparison, startsWith, etc. I haven't tested but you could probably nest filters too for querying inner collections.
您可以在委托中放置尽可能多的条件:简单比较、startsWith 等。我还没有测试过,但您也可以嵌套过滤器来查询内部集合。
回答by Ela Bednarek
ObjectPathis simple and ligthweigth query language for JSON documents of complex or unknown structure. It's similar to XPath or JSONPath, but much more powerful thanks to embedded arithmetic calculations, comparison mechanisms and built-in functions.
ObjectPath是复杂或未知结构的JSON文件简单ligthweigth查询语言。它类似于 XPath 或 JSONPath,但由于嵌入式算术计算、比较机制和内置函数而更加强大。


Python version is mature and used in production. JS is still in beta.
Python 版本已经成熟并用于生产。JS 仍处于测试阶段。
Probably in the near future we will provide a full-fledged Javascript version. We also want to develop it further, so that it could serve as a simpler alternative to Mongo queries.
可能在不久的将来,我们将提供一个成熟的 Javascript 版本。我们还希望进一步开发它,以便它可以作为 Mongo 查询的更简单的替代方案。
回答by James Newton-King
回答by Alexx Roche
回答by deitch
OK, this post is a little old, but... if you want to do SQL-like query in native JSON (or JS objects) on JS objects, take a look at https://github.com/deitch/searchjs
好吧,这篇文章有点旧,但是……如果你想在原生 JSON(或 JS 对象)中对 JS 对象进行类似 SQL 的查询,请查看https://github.com/deitch/searchjs
It is both a jsql language written entirely in JSON, and a reference implementation. You can say, "I want to find all object in an array that have name==="John" && age===25 as:
它既是一种完全用 JSON 编写的 jsql 语言,也是一种参考实现。你可以说,“我想在一个数组中找到所有具有 name==="John" && age===25 的对象:
{name:"John",age:25,_join:"AND"}
The reference implementation searchjs works in the browser as well as as a node npm package
参考实现 searchjs 在浏览器以及 node npm 包中工作
npm install searchjs
It can also do things like complex joins and negation (NOT). It natively ignores case.
它还可以执行诸如复杂连接和否定 (NOT) 之类的操作。它本身忽略大小写。
It doesn't yet do summation or count, but it is probably easier to do those outside.
它还没有做求和或计数,但在外面做这些可能更容易。

