首页 > 解决方案 > 如何将多记录多行 JSON 转换为 AWS Athena 的每记录 JSON 单行?

问题描述

我想在 AWS Athena 中使用 json 文件,但 Athena 不支持多行 JSON。

我有以下(其中一个值是 XML)

{
  "id" : 10,
  "name" : "bob",
  "data" : "<some> \n <xml> \n <in here>"
},
{
  "id" : 20,
  "name" : "jane",
  "data" : "<other> \n <xml> \n <in here>"
}

我需要以下雅典娜

{ "id" : 10, "name" : "bob", "data" : "<some> <xml> <in here>" },
{ "id" : 20, "name" : "jane", "data" : "<other> <xml> <in here>" }

我正在使用 RazorSQL 从 DB2 中导出数据,并尝试使用 Python 编写一些代码来“扁平化”它,但还没有成功。

谢谢!

标签: pythonsqljsonaws-lambdaamazon-athena

解决方案


我最终做了一些快速而肮脏的事情

import json
with open('data.json') as jfile:
    data = json.load(jfile)
    for d in data:
        print(json.dumps(d) + ',')

哪个打印

{'id': 200, 'name': 'bob', 'data': '<other> \n <xml> \n <data>'},
{"id": 200, "name": "bob", "data": "<other> \n <xml> \n <data>"},

刚刚将输出保存到另一个文件:P

它失败了,因为文件太大了,但是嘿..很接近!


推荐阅读