我有一个获取嵌套对象并删除所有嵌套(使对象平坦)的代码:
def flatten_json(y):
"""
@param y: Unflated Json
@return: Flated Json
"""
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
out[name[:-1]] = x
else:
out[name[:-1]] = x
flatten(y)
return out
def generatejson(response):
sample_object = pd.DataFrame(response.json())['results'].to_dict()
flat = {k: flatten_json(v) for k, v in sample_object.items()}
return json.dumps(flat, sort_keys=True)
respons= requests.get(urlApi, data=data, headers=hed, verify=False)
flat1 = generatejson(respons)
....
storage.Bucket(BUCKET_NAME).item(path).write_to(flat1, 'application/json')
这将执行以下操作:
这很好。问题在于BigQuery不支持Json,因此我需要在上传之前将其转换为换行Json标准格式。
是否有一种方法可以更改返回json.dumps(flat, sort_keys=True)
,以便它将返回新的Json格式而不是常规Json?
我的杰森样品:
{"0": {"code": "en-GB", "id": 77, "languageName": "English", "name": "English"},
"1": {"code": "de-DE", "id": 78, "languageName": "Deutsch", "name": "German"}}
编辑:
新行json的预期结果是:
{"languageName":"English","code":"en-GB","id":2,"name":"English"}
{"languageName":"Deutsch","code":"de-DE","id":5,"name":"German"}
例如,如果我接受API调用并这样做:
df['results'].to_json(orient="records",lines=True)
这将提供所需的输出。但是我无法使用json.dumps(flat, sort_keys=True)
来做到这一点,那里没有使用数据框。
答案 0 :(得分:1)
我认为您正在寻找类似的东西?
import json
def create_jsonlines(original):
if isinstance(original, str):
original = json.loads(original)
return '\n'.join([json.dumps(original[outer_key], sort_keys=True)
for outer_key in sorted(original.keys(),
key=lambda x: int(x))])
# Added fake record to prove order is sorted
inp = {
"3": {"code": "en-FR", "id": 76, "name": "French", "languageName": "French"},
"0": {"code": "en-GB", "id": 77, "languageName": "English", "name": "English"},
"1": {"code": "de-DE", "id": 78, "languageName": "Deutsch", "name": "German"}
}
output = create_jsonlines(inp)
print(output)
答案 1 :(得分:0)