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


当前回答

如果你使用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相同的感觉。

其他回答

我使用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 

谷歌有一个项目叫lovefield;刚刚发现了它,它看起来很有趣,尽管它比键入下划线或lodash更复杂。 https://github.com/google/lovefield

Lovefield是一个用纯JavaScript编写的关系查询引擎。它 还提供了在浏览器端持久化数据的帮助。 使用IndexedDB本地存储数据。它提供了类似sql的语法和 跨浏览器工作(目前支持Chrome 37+, Firefox 31+, IE 10+, Safari 5.1+…


这个领域最近出现的另一个有趣的条目叫做jinqJs。 http://www.jinqjs.com/ 简单回顾一下示例,它看起来很有前途,API文档似乎写得很好。


function isChild(row) {
  return (row.Age < 18 ? 'Yes' : 'No');
}

var people = [
  {Name: 'Jane', Age: 20, Location: 'Smithtown'},
  {Name: 'Ken', Age: 57, Location: 'Islip'},
  {Name: 'Tom', Age: 10, Location: 'Islip'}
];

var result = new jinqJs()
  .from(people)
  .orderBy('Age')
  .select([{field: 'Name'}, 
     {field: 'Age', text: 'Your Age'}, 
     {text: 'Is Child', value: isChild}]);

jinqJs is a small, simple, lightweight and extensible javaScript library that has no dependencies. jinqJs provides a simple way to perform SQL like queries on javaScript arrays, collections and web services that return a JSON response. jinqJs is similar to Microsoft's Lambda expression for .Net, and it provides similar capabilities to query collections using a SQL like syntax and predicate functionality. jinqJs’s purpose is to provide a SQL like experience to programmers familiar with LINQ queries.

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

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

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

在MongoDB中,这就是它的工作方式(在mongo shell中,存在您选择的语言的驱动程序)。

db.collection.insert({"x": 2, "y": 0}); // notice the ':' instead of ','
db.collection.insert({"x": 3, "y": 1});
db.collection.insert({"x": 4, "y": 1});

db.collection.aggregate([{$match: {"y": {$gt: 0}}}, 
                         {$group: {_id: "sum", sum: {$sum: "$x"}}}]);
db.collection.aggregate([{$match: {"y": {$gt: 0}}}, 
                         {$group: {_id: "list", list: {$push: "$x"}}}]);

前三个命令将数据插入到集合中。(只需启动mongod服务器并连接mongo客户端。)

接下来的两个处理数据。$match过滤,$group分别应用sum和list。

当前的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/上下载和讨论