JSON 是否有查询语言? [英] Is there a query language for JSON?

查看:18
本文介绍了JSON 是否有查询语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有(大致)类似于 SQL 或 XQuery 的语言来查询 JSON?

Is there a (roughly) SQL or XQuery-like language for querying JSON?

我正在考虑可以很好地映射到 JSON 的非常小的数据集,以便轻松回答诸如Y > 3 时 X 的所有值是多少"之类的查询或执行通常的 SUM/COUNT 类型操作.

I'm thinking of very small datasets that map nicely to JSON where it would be nice to easily answer queries such as "what are all the values of X where Y > 3" or to do the usual SUM / COUNT type operations.

作为完全虚构的示例,如下所示:

As completely made-up example, something like this:

[{"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)

I'm thinking this would work both client-side and server-side with results being converted to the appropriate language-specific data structure (or perhaps kept as JSON)

快速谷歌搜索表明人们已经考虑过并实施了一些事情(JAQL),但似乎还没有出现标准用法或库集.虽然每个功能单独实现都相当简单,但如果有人已经做对了,我不想重新发明轮子.

A quick Googling suggests that people have thought about it and implemented a few things (JAQL), but it doesn't seem like a standard usage or set of libraries has emerged yet. While each function is fairly trivial to implement on its own, if someone has already done it right I don't want to re-invent the wheel.

有什么建议吗?

这可能确实是一个坏主意,或者 JSON 可能是我所想的过于通用的格式.想要一种查询语言而不是根据需要直接执行 summing/etc 函数的原因是我希望根据用户输入动态构建查询.有点像我们不需要 SQL,我们可以编写我们需要的函数"的论点.最终,要么失控,要么您最终编写自己的 SQL 版本,因为您将其推得越来越远.(好吧,我知道这有点愚蠢,但你明白了..)

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

推荐答案

好的,怎么样:

它们似乎都在进行中,但在某种程度上是可行的.它们在概念上也类似于 XPath 和 XQuery;即使 XML 和 JSON 具有不同的概念模型(分层与对象/结构).

They all seem to be a bit work in progress, but work to some degree. They are also similar to XPath and XQuery conceptually; even though XML and JSON have different conceptual models (hierarchic vs object/struct).

编辑 2015 年 9 月:实际上现在有 JSON 指针 标准,允许非常简单有效地遍历 JSON 内容.它不仅被正式指定,而且被许多 JSON 库支持.因此,我将其称为实际真正有用的标准,尽管由于其表达能力有限,它本身可能被视为查询语言,也可能不被视为查询语言.

EDIT Sep-2015: Actually there is now JSON Pointer standard that allows very simple and efficient traversal of JSON content. It is not only formally specified, but also supported by many JSON libraries. So I would call it actual real useful standard, although due to its limited expressiveness it may or may not be considered Query Language per se.

这篇关于JSON 是否有查询语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆