是否有(大致)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..)


当前回答

编辑 2022 年 9 月:

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

原:

当然可以,这样怎么样:

JsonPath。 Json查询

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

2015年9月:

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

其他回答

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

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

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

我使用SQLite: https://sqlite.org/json1.html

这很好,因为你可以使用实际的SQL语言,SQLite非常快。

首先我创建一个临时表:

create temp table data as select value from json_each(readfile('data.json'))

然后使用SQLite JSON函数:

select value->'$.foo' foo, count(value->'$.bar') nbar from data group by foo 

JMESPath工作起来非常简单和良好:http://jmespath.org/。 它有一个完整的规范和多种语言的库。亚马逊在AWS命令行界面中使用它,所以它必须非常稳定。

语法的例子:

// Select a single item
people[1].firstName

// Select a slice of an array
people[0:5]

// Select all the first names
people[*].firstName

// Select all first names based on search term
people[?state=='VA'].firstName

// Count how many people are over 35
length(people[?age>`35`])

// Select only the name and age of people over 35
people[?age>`35`].{name: name, age: age}

// Join expressions together to sort and join elements into a string
people[?state == 'WA'].name | sort(@) | join(', ', @)

在文档中还有更多的实例可以使用。

只要有可能,我就会将所有查询转移到服务器的后端(SQL DB或其他本地数据库类型)。原因是这样查询会更快更优化。

我知道jSON可以是独立的,对于查询语言可能有+/-,但如果您从后端检索数据到浏览器,我看不出它的优势,因为大多数jSON用例都是这样。在后端进行查询和筛选,以获得所需的尽可能小的数据。

如果出于某种原因,你需要在前端进行查询(主要是在浏览器中),那么我建议只使用数组。过滤器(为什么要发明其他东西?)

也就是说,我认为更有用的是json的转换API…它们更有用,因为一旦你有了数据,你可能想要以多种方式显示它。但是,同样,如果您使用的是server<——>客户端模型,那么您可以在服务器上比在客户端上更容易地完成这些工作。

只值我的两便士!

下面是一些简单的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'