是否有(大致)SQL或类似xquery的语言来查询JSON?

我正在考虑非常小的数据集,很好地映射到JSON,它将很容易回答查询,如“X的所有值是什么,Y > 3”或做通常的SUM / COUNT类型操作。

作为一个完全虚构的例子,是这样的:

[{"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])

我认为这将在客户端和服务器端工作,结果将被转换为适当的特定于语言的数据结构(或者可能保留为JSON)

快速搜索一下谷歌,就会发现人们已经考虑过它并实现了一些东西(JAQL),但它似乎还没有一个标准的用法或库集出现。虽然单独实现每个功能都是相当琐碎的,但如果有人已经做对了,我就不想重新发明轮子。

有什么建议吗?

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..)


当前回答

下面是一些简单的javascript库,也可以做到这一点:

Dollar Q is a nice lightweight library. It has a familiar feel to the chaining syntax made popular by jQuery and is only 373 SLOC. SpahQL is a fully featured query language with a syntax similar to XPath (Homepage, Github jFunk is an in progress query language, with a syntax similar to CSS/jQuery selectors. It looked promising, but hasn't had any development beyond its in initial commit. (added 2014): the jq command line tool has a neat syntax, but unfortunately it is a c library. Example usage: < package.json jq '.dependencies | to_entries | .[] | select(.value | startswith("git")) | .key'

其他回答

内置的array.filter()方法使大多数所谓的javascript查询库过时

你可以在委托中放入尽可能多的条件:简单的比较,startsWith,等等。我还没有测试过,但是您可能也可以嵌套过滤器来查询内部集合。

当前的Jaql实现目标是使用Hadoop集群进行大型数据处理,因此可能超出了您的需求。然而,它可以在没有Hadoop集群的情况下轻松运行(但仍然需要编译Hadoop代码及其依赖项,其中大部分都包括在内)。Jaql的一个小实现可以嵌入到Javascript和浏览器中,这将是对项目的一个很好的补充。

上面的例子很容易用jaql编写:

$data = [{"x": 2, "y": 0}, {"x": 3, "y": 1}, {"x": 4, "y": 1}];

$data -> filter $.y > 0 -> transform $.x -> sum(); // 7

$data -> filter $.y > 0 -> transform $.x; // [3,4]

当然,还有更多。例如:

// Compute multiple aggregates and change nesting structure:
$data -> group by $y = $.y into { $y, s:sum($[*].x), n:count($), xs:$[*].x}; 
    // [{ "y": 0, "s": 2, "n": 1, "xs": [2]   },
    //  { "y": 1, "s": 7, "n": 2, "xs": [3,4] }]

// Join multiple data sets:
$more = [{ "y": 0, "z": 5 }, { "y": 1, "z": 6 }];
join $data, $more where $data.y == $more.y into {$data, $more};
    // [{ "data": { "x": 2, "y": 0 }, "more": { "y": 0, "z": 5 }},
    //  { "data": { "x": 3, "y": 1 }, "more": { "y": 1, "z": 6 }},
    //  { "data": { "x": 4, "y": 1 }, "more": { "y": 1, "z": 6 }}]

Jaql可以在http://code.google.com/p/jaql/上下载和讨论

PythonQL提供了一种嵌入式语法,IMHO是对SQL的改进,主要是因为group、window、where、let等可以自由混合。

$ cat x.py
#coding: pythonql
data = [{"x": 2, "y": 0}, {"x": 3, "y": 1}, {"x": 4, "y": 1}]
q = [x match {'x': as x, 'y': as y} in data where y > 0]
print(sum(q))
print(list(q))

q = [x match {'x': as x, 'y': as y} as d in data where d['y'] > 0]
print(sum(q))

这段代码显示了您的问题的两个不同答案,这取决于您需要处理整个结构或仅处理值。执行会给你预期的结果。

$ python x.py
7
[3, 4]
7

编辑 2022 年 9 月:

JMESPath似乎是使用最广泛、增长最快、评价最好的替代方案。它有很多功能,包括“where”风格的过滤器。

原:

当然可以,这样怎么样:

JsonPath。 Json查询

它们似乎都在进行中,但在某种程度上是有效的。它们在概念上也类似于XPath和XQuery;尽管XML和JSON有不同的概念模型(层次结构vs对象/结构)。

2015年9月:

实际上,现在有一个JSON指针标准,可以非常简单有效地遍历JSON内容。它不仅是正式指定的,而且许多JSON库也支持它。所以我将它称为真正有用的标准,尽管由于其表达能力有限,它本身可能被认为是查询语言,也可能不被认为是查询语言。

ObjectPath是用于复杂或结构未知的JSON文档的简单而轻量级的查询语言。它类似于XPath或JSONPath,但由于嵌入了算术计算、比较机制和内置函数,它的功能要强大得多。

Python版本已经成熟,已用于生产环境。JS仍处于测试阶段。

在不久的将来,我们可能会提供一个完整的Javascript版本。我们还想进一步开发它,以便它可以作为Mongo查询的一个更简单的替代品。