使用Python将Json转换为换行Json标准 [英] Convert Json to newline Json standard using Python

查看:73
本文介绍了使用Python将Json转换为换行Json标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个获取嵌套对象并删除所有嵌套(使对象平坦)的代码:

I have a code which get nested object and remove all nesting (make the object flat):

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

这将执行以下操作:

  1. 从API调用
  2. 删除嵌套对象
  3. 生成json
  4. 将json上载到Google Storage.

这很好.问题在于BigQuery不支持Json,因此我需要在上传之前将其转换为换行Json标准格式.

This works great. The problem is that BigQuery does not support Json so I need to convert it to newline Json standard format before the upload.

是否可以更改return json.dumps(flat, sort_keys=True),以便它将返回新的Json格式而不是常规Json?

Is there a way to change return json.dumps(flat, sort_keys=True) so it will return the new Json format and not regular Json?

我的杰森样品:

{"0": {"code": "en-GB", "id": 77, "languageName": "English", "name": "English"}, 
"1": {"code": "de-DE", "id": 78, "languageName": "Deutsch", "name": "German"}}

新行json的预期结果是:

the expected result is of the new line json is:

{"languageName":"English","code":"en-GB","id":2,"name":"English"}
{"languageName":"Deutsch","code":"de-DE","id":5,"name":"German"}

例如,如果我接受API调用并这样做:

For example if I take the API call and do:

df['results'].to_json(orient="records",lines=True)

这将提供所需的输出.但是我无法使用json.dumps(flat, sort_keys=True)来实现,因此那里没有使用数据框.

This will give the desired output. but I can't do that with json.dumps(flat, sort_keys=True) there is no use of dataframe there.

推荐答案

我认为您正在寻找类似的东西?

I think you're looking for something like this?

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)

这篇关于使用Python将Json转换为换行Json标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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