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


当前回答

如果你使用的是。net,那么Json。NET支持JSON之上的LINQ查询。这篇文章有一些例子。它支持过滤、映射、分组等。

其他回答

我推荐我正在做的项目jLinq。我在寻求反馈,所以我很有兴趣听听你的想法。

如果让你写查询类似于你会在LINQ…

var results = jLinq.from(records.users)

    //you can join records
    .join(records.locations, "location", "locationId", "id")

    //write queries on the data
    .startsWith("firstname", "j")
    .or("k") //automatically remembers field and command names

    //even query joined items
    .equals("location.state", "TX")

    //and even do custom selections
    .select(function(rec) {
        return {
            fullname : rec.firstname + " " + rec.lastname,
            city : rec.location.city,
            ageInTenYears : (rec.age + 10)
        };
    });

它也是完全可扩展的!

文档仍在编写中,但您仍然可以在线尝试。

谷歌有一个项目叫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.

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

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

另一种方法是使用mongoDB,你可以将JSON存储在mongoDB中,然后通过mongoDB查询语法进行查询。

如果你想使用纯javascript试试这个:

Var对象= {result: {data: {point1: "x", value: 2}, foo: {bar: 7}}}, Path = 'result.data.value', getValue = (o, p) => p.split('.')。Reduce ((r, k) => r[k], o); console.log (getValue(对象,路径));