允许对 JSON 数据进行类似 SQL 查询的 Javascript 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4720494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:45:39  来源:igfitidea点击:

Javascript libraries that allow for SQL-like queries on JSON data?

javascriptjquerysqlmysqljson

提问by b_dev

Say our JSON data comes from a single MySQL table:

假设我们的 JSON 数据来自单个 MySQL 表:

someJSON =    [ { name: 'bill' , sex:'M', income:50000 },
                { name: 'sara' , sex:'F', income:100000 },
                 ...
               ];

And say the pseudo-code is:

并说伪代码是:

"Get all the person objects of all sex:Fof income> 60000`".

“获取所有的所有的人对象sex:Fincome> 60000`”。

Are there any javascript libraries that would allow one to code such queries on this JSON data using a SQL or SQL-like syntax.

是否有任何 javascript 库允许人们使用 SQL 或类似 SQL 的语法对此 JSON 数据进行编码此类查询。

In case you are curious, some context:

如果你很好奇,一些上下文:

I am making the front-end of a data analysis web service for my organization without knowing what the future backend will be. In the future they will migrate their data from MS Access tables to some-sort of MySQL-type database. Until then I am using static JSON files to start development and was thinking it may be helpful for them in the future to have my javascript queries appear as MySQL queries. (The current MS Access database is unreachable from the web.)

我正在为我的组织制作数据分析 Web 服务的前端,但不知道未来的后端是什么。将来,他们会将他们的数据从 MS Access 表迁移到某种 MySQL 类型的数据库。在那之前,我使用静态 JSON 文件开始开发,并认为将来让我的 javascript 查询显示为 MySQL 查询可能对他们有所帮助。(当前的 MS Access 数据库无法从 Web 访问。)

采纳答案by Mike Valenty

Check out jslinq:

查看jslinq

var myList = [
            {FirstName:"Chris",LastName:"Pearson"},
            {FirstName:"Kate",LastName:"Johnson"},
            {FirstName:"Josh",LastName:"Sutherland"},
            {FirstName:"John",LastName:"Ronald"},
            {FirstName:"Steve",LastName:"Pinkerton"}
            ];

var exampleArray = JSLINQ(myList)
                   .Where(function(item){ return item.FirstName == "Chris"; })
                   .OrderBy(function(item) { return item.FirstName; })
                   .Select(function(item){ return item.FirstName; });

回答by agershun

You can try alasql.js. It is pure JavaScript client-side SQL-server, where you can do queries over JSON objects.

你可以试试alasql.js。它是纯 JavaScript 客户端 SQL 服务器,您可以在其中对 JSON 对象进行查询。

   // Fill table with data
   var data = [ { name: 'bill' , sex:'M', income:50000 },
                { name: 'sara' , sex:'F', income:100000 }];

   // Do the query
   console.log(alasql("SELECT * FROM ? WHERE sex='F' AND income > 60000",[data]));

Try this in Fiddle

小提琴中试试这个

回答by arpo

I use Taffydb. TaffyDB is an opensouce library that brings database features into your JavaScript applications. http://taffydb.com/

我用的是 Taffydb。TaffyDB 是一个开源库,可以将数据库功能带入您的 JavaScript 应用程序。 http://taffydb.com/

回答by goat

I've seen a few linq like javascript libraries in past google searches.

我在过去的谷歌搜索中看到了一些类似 javascript 库的 linq。

Edit- here's a couple
http://linqjs.codeplex.com/
http://jslinq.codeplex.com/
http://jsinq.codeplex.com/<-- really cool playground for this one

编辑-这里有几个
http://linqjs.codeplex.com/
http://jslinq.codeplex.com/
http://jsinq.codeplex.com/<--非常酷的游乐场

回答by jfraber

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
 <script type="text/javascript" src="linq.js"></script>
 <script type="text/javascript">
var jsonArray = [
    { "user": { "id": 100, "screen_name": "d_linq" }, "text": "to objects" },
    { "user": { "id": 130, "screen_name": "c_bill" }, "text": "g" },
    { "user": { "id": 155, "screen_name": "b_mskk" }, "text": "kabushiki kaisha" },
    { "user": { "id": 301, "screen_name": "a_xbox" }, "text": "halo reach" }
]
// ["b_mskk:kabushiki kaisha", "c_bill:g", "d_linq:to objects"]
var queryResult = Enumerable.From(jsonArray)
    .Where(function (x) { return x.user.id < 200 })
    .OrderBy(function (x) { return x.user.screen_name })
    .Select(function (x) { return x.user.screen_name + ':' + x.text })
    .ToArray();
// shortcut! string lambda selector
var queryResult2 = Enumerable.From(jsonArray)
    .Where("$.user.id < 200")
    .OrderBy("$.user.screen_name")
    .Select("$.user.screen_name + ':' + $.text")
    .ToArray();

console.log(queryResult);
console.log(queryResult2);
 </script>
</head>
<body>


</body>
</html>

回答by Gregor

I know the question is old but I just came here through a Google search. I'm just following a talk about objeq. Looks pretty promising and very much what you are searching for.

我知道这个问题很老,但我只是通过谷歌搜索来到这里。我只是在关注objeq。看起来很有前途,而且非常符合您的要求。

回答by Kyle Wild

You may be interested in checking out MongoDB, a JSON-style data store with full queryability. Here is its query syntax:

您可能有兴趣查看 MongoDB,这是一种具有完全可查询性的 JSON 样式数据存储。这是它的查询语法:

db.users.find({'last_name': 'Smith'})

For your example question:

对于您的示例问题:

db.users.find({'sex': 'F', 'income' : {$gt : 60000}})

回答by Matthew Manela

There is also JsonSqlwhich seems to be similar like what you are looking for. Only problem is that it hasn't been updated in 12/30/2007. Still the code is there to grab and play with.

还有JsonSql似乎与您正在寻找的相似。唯一的问题是它在 2007 年 12 月 30 日没有更新。代码仍然可以抓取和使用。

回答by Phrogz

Depending on what browsers/versions you must support, I would strive to use HTML5 client-side SQL, pushing my JSON data into one or more tables and harnessing the power of true SQL queries.

根据您必须支持的浏览器/版本,我将努力使用HTML5 客户端 SQL,将我的 JSON 数据推送到一个或多个表中,并利用真正的 SQL 查询的功能。

Here's the draft spec: http://www.w3.org/TR/webdatabase/

这是规范草案:http: //www.w3.org/TR/webdatabase/

回答by Daveo

There is also a XPath style query called JSONPath that I like http://goessner.net/articles/JsonPath/

还有一个名为 JSONPath 的 XPath 样式查询,我喜欢http://goessner.net/articles/JsonPath/

And there is this http://code.google.com/p/jfunk/Which users jQuery style selectors to filter data

还有这个http://code.google.com/p/jfunk/哪些用户使用 jQuery 样式选择器来过滤数据