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

时间:2021-08-11 16:22:55

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:F of income > 60000`".

“获得所有人所有的性对象:F收入bb60000 '”。

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访问表迁移到某种sql类型的数据库。在此之前,我一直在使用静态JSON文件开始开发,并认为将来让我的javascript查询显示为MySQL查询可能会对他们有所帮助。(目前的MS Access数据库在web上是不可访问的。)

10 个解决方案

#1


13  

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; });

#2


8  

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

我使用Taffydb。TaffyDB是一个opensouce库,它将数据库特性引入到JavaScript应用程序中。http://taffydb.com/

#3


8  

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 server,在这里您可以对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

试试这个在小提琴

#4


3  

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

在过去的谷歌搜索中,我看到过一些linq,比如javascript库。

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/ <-这是一个很酷的游戏

#5


1  

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}})

#6


1  

There is also JsonSql which 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日没有更新。仍然有代码可以抓取和使用。

#7


1  

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

还有一个XPath样式查询,名为JSONPath,我喜欢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样式选择器来过滤数据

#8


1  

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/

#9


1  

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的演讲。看起来很有前途,而且是你正在寻找的。

#10


1  

<!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>

#1


13  

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; });

#2


8  

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

我使用Taffydb。TaffyDB是一个opensouce库,它将数据库特性引入到JavaScript应用程序中。http://taffydb.com/

#3


8  

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 server,在这里您可以对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

试试这个在小提琴

#4


3  

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

在过去的谷歌搜索中,我看到过一些linq,比如javascript库。

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/ <-这是一个很酷的游戏

#5


1  

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}})

#6


1  

There is also JsonSql which 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日没有更新。仍然有代码可以抓取和使用。

#7


1  

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

还有一个XPath样式查询,名为JSONPath,我喜欢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样式选择器来过滤数据

#8


1  

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/

#9


1  

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的演讲。看起来很有前途,而且是你正在寻找的。

#10


1  

<!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>