如何在JSON YQL请求中过滤属性?

时间:2017-05-09 19:21:51

标签: json yql

我使用Yahoo Query Language(YQL)从不支持CORS的Web服务检索JSON并过滤每个响应对象的属性,以便响应JSON中的每个对象只包含属性我需要。

从Web服务返回的JSON如下所示:

{
    "results": [
        {},
        {},
        ...
    ]
}

我的查询目前看起来像这样:

select results from json where url="..."

这将返回results属性数组。如何修改"选择"查询或命令的其他部分只选择results数组中每个结果对象的某些属性?我很难从documentation找到这个。

1 个答案:

答案 0 :(得分:3)

在您的情况下,您应将查询设置为"results.name, results.number"

让我们看一个请求占位符JSON(https://jsonplaceholder.typicode.com/posts)的示例:

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  },
  ...
]

在此示例中,root是一个数组,我们可以使用此YQL查询过滤数组中每个对象所具有的属性:

select json.userId, json.title from json where url='https://jsonplaceholder.typicode.com/posts'

这给出了这个JSON响应:

      "results": {
       "json": [
        {
         "json": {
          "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
          "userId": "1"
         }
        },
        .
        .
        .