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


当前回答

jq是一种JSON查询语言,主要用于命令行,但绑定到广泛的编程语言(Java, node.js, php,…),甚至可以通过jq-web在浏览器中使用。基于c语言的jq实现通常被称为“jq”,基于go语言的版本被称为“gojq”。

以下是基于原始问题的一些插图,以JSON为例:

 [{"x": 2, "y": 0}}, {"x": 3, "y": 1}, {"x": 4, "y": 1}]

SUM(X) WHERE Y > 0(等于7)

map(select(.y > 0)) | add

LIST(X) WHERE Y > 0(等于[3,4])

map(.y > 0)

jq语法扩展了JSON语法

每个JSON表达式都是一个有效的jq表达式,像[1,(1+1)]和{"a":(1+1)} '这样的表达式说明了jq如何扩展JSON语法。

一个更有用的例子是jq表达式:

{a,b}

,考虑到JSON值{“a”:1、“b”:2,“c”:3},等于{“a”:1、“b”:2}。

其他回答

更新:XQuery 3.1可以查询XML或JSON—或者同时查询两者。XPath 3.1也可以。

这个名单还在增加:

JSONiq(基于XQuery) UNQL(类似SQL) JaQL(功能) JsonPath(于xpath) Json查询(类似于xpath) GraphQL(基于模板,类型化)

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

你可以使用linq.js。

这允许从对象的数据集中使用聚合和选择,就像其他结构数据一样。

var data = [{x: 2, y: 0}, {x: 3 y: 1}, {x: 4, y: 1}); // sum (x) where y > 0 -> console.log (Enumerable.From(数据)其中(" $。y > 0").Sum("$.x")); // list (x) where y > 0 -> [3,4] console.log (Enumerable.From(数据)其中(" $。y > 0").Select("$.x").ToArray()); < script src = " https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js " > < /脚本>

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

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(', ', @)

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