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

其他回答

好吧,这篇文章有点老了,但是……如果你想用原生JSON(或JS对象)对JS对象进行类似sql的查询,请查看https://github.com/deitch/searchjs

它既是一种完全用JSON编写的jsql语言,也是一种参考实现。你可以说,“我想在数组中找到所有对象,其name==="John" && age===25 as:

{name:"John",age:25,_join:"AND"}

参考实现searchjs可以在浏览器中工作,也可以作为节点npm包工作

npm install searchjs

它还可以做复杂连接和否定(NOT)之类的事情。它本身就忽略了大小写。

它还没有做求和或计数,但在外面做这些可能更容易。

如果你使用python, MongoDB有迷你开源版本, MontyDB https://github.com/davidlatwe/montydb 它在github上有500多颗星,并且得到了JetBrains的支持。

from montydb.utils import MontyList
response = [
    {'namespace': 'dash_mantine_components',
     'props': {'checked': True,
               'id': {'index': 0, 'type': 'checkbox'},
               'label': 'My first to do'},
     'type': 'Checkbox'},
    {'namespace': 'dash_mantine_components',
     'props': {'checked': True,
               'id': {'index': 1, 'type': 'checkbox'},
               'label': 'My Another to do'},
     'type': 'Input'}, 
    {'namespace': 'dash_mantine_components',
     'props': {'checked': False,
               'id': {'index': 2, 'type': 'checkbox'},
               'label': 'My next level to do'},
     'type': 'Div'}, 
]

cli = MontyList(response)
cli.find({'props.checked': True},{'type':1})

输出:

MontyList([{'type': 'Checkbox'}, {'type': 'Input'}])

与其他jsonquery选项相比,我更喜欢这个库,因为这个库提供了与MongoDB相同的感觉。

我赞同只使用自己的javascript,但对于更复杂的东西,您可以查看dojo数据。还没有使用过,但看起来它大致提供了您正在寻找的那种查询接口。

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

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

查看https://github.com/niclasko/Cypher.js(注:我是作者)

它是Cypher图形数据库查询语言和图形数据库的零依赖Javascript实现。它运行在浏览器(测试与Firefox, Chrome, IE)。

与问题相关。它可以用来查询JSON端点:

load json from "http://url/endpoint" as l return l limit 10

下面是一个查询复杂JSON文档并对其进行分析的示例:

js JSON查询示例