首页 > 解决方案 > JSONPaths 文件:解析 JSON 数组中包含的 JSON 对象

问题描述

我有以下 JSON 形式的行:

 [
  {
    "id": 1,
    "costs": [
      {
        "blue": 100,
        "location":"courts",
        "sport": "football"
      }
    ]
  }
]

我想将其上传到红移表中,如下所示:

  id  | blue  | location | sport
--------+------+---------+------
   1  | 100   | courts    |football

以下 JSONPaths 文件不成功:

{
    "jsonpaths": [
        "$.id",
        "$.costs[0].blue",
        "$.costs[0].location",
        "$.costs[0].sport"
    ]
}

Redshift 返回以下错误代码:

err_code: 1216 Invalid JSONPath format: Member is not an object.  

如何更改 jsonpaths 文件以便能够根据需要上传 json?

标签: jsonamazon-redshift

解决方案


John Rotenstein在评论中提供了答案。我只是在这里正式确定答案。

文档中所示,输入 JSON 记录必须是 JSON 对象的新行分隔序列。这些示例将 JSON 对象显示为打印得很漂亮,但通常记录的输入流将是每行一个 JSON 对象。

{ "id": 1, "costs": [ { "blue": 100, "location":"courts", "sport": "football" } ] }
{ "id": 2, "costs": [ { "blue": 200, "location":"fields", "sport": "cricket" } ] }

因此,从技术上讲,输入记录流不需要是有效的 JSON,而是分隔的有效 JSON 对象流。


推荐阅读