Python用Python修改JSON的结构(BQ接受)并保存

Python用Python修改JSON的结构(BQ接受)并保存,python,json,Python,Json,我想问一下,是否有一种使用Python修改JSON的简单方法 我找到了一些相关的话题,但无法找到解决当前问题的方法 目前,JSON如下所示: { "X": [ { "sample_topic_x":"sample_content_x_1", ... } { "sample_topic_x":"sample_conte

我想问一下,是否有一种使用Python修改JSON的简单方法

我找到了一些相关的话题,但无法找到解决当前问题的方法

目前,JSON如下所示:

    {
         "X": [
           {
               "sample_topic_x":"sample_content_x_1",
               ...
           }
           {
               "sample_topic_x":"sample_content_x_2",
               ...
           }
           ......
         ]
         "Y": [
           {
               "sample_topic_y":"sample_content_y_1",
               ...
           }
           {
               "sample_topic_y":"sample_content_y_2",
               ...
           }
           ......
         ]
}
必需:BQ接受/需要删除“Y”,在此格式中仅保留“X”

{"sample_topic_x":"sample_content_x_1",.....}
{"sample_topic_x":"sample_content_x_2",.....}
{"sample_topic_x":"sample_content_x_3",.....}
有相关的文档、主题吗

p.S>更新1.0

import json
json_path = 'C:\XXX\exportReport.json'

def updateJsonFile():
jsonFile = open(json_path, "r") # Open the JSON file for reading
data = json.load(jsonFile) # Read the JSON into the buffer
jsonFile.close() # Close the JSON file

updateJsonFile()

首先需要提取父数据。并将其定义为一个变量。并在此变量中搜索“X”数据

解决方案:

import json
json_path = 'C:\XXX\exportReport.json'
output_path = 'C:\XXX\your_output_file.txt'

with open(json_path) as f:
    data = json.loads(f.read())

# Opening output file in append mode
# Note: Output file is not JSON, as the required format is not valid json
with open(output_file, "a+") as op:
    for element in data.get('X'):
        op.write(json.dumps(element) + "\n")
说明:

import json
json_path = 'C:\XXX\exportReport.json'
output_path = 'C:\XXX\your_output_file.txt'

with open(json_path) as f:
    data = json.loads(f.read())

# Opening output file in append mode
# Note: Output file is not JSON, as the required format is not valid json
with open(output_file, "a+") as op:
    for element in data.get('X'):
        op.write(json.dumps(element) + "\n")
使用
json.loads
加载输入json文件。输出文件将是纯文本文件,而不是JSON文件,因为所需的输出格式不是有效的JSON。使用
.txt
文件存储输出。在数据中存储
json.loads()的值。要获取内部元素X(字典列表),请使用
data.get('X')
,它将返回列表。迭代并将
json.dumps()
写入输出文件,每个元素换行

C:\XXX\exportReport.json

{

    "X": [
       {
           "sample_topic_x":"sample_content_x_1",
           ...
       }
       {
           "sample_topic_x":"sample_content_x_2",
           ...
       }
       ......
     ]
     "Y": [
       {
           "sample_topic_y":"sample_content_y_1",
           ...
       }
       {
           "sample_topic_y":"sample_content_y_2",
           ...
       }
       ......
     ]
}
C:\XXX\your\u output\u file.txt

{"sample_topic_x":"sample_content_x_1",.....}
{"sample_topic_x":"sample_content_x_2",.....}
{"sample_topic_x":"sample_content_x_3",.....}

我理解这样做的逻辑,但是您知道关于使用Python修改JSON结构的文档吗?由于我找不到任何相关的源代码来修改结构,但很多都是关于更改内容的。TypeError:expected string或bufferYes it’s does)),解决方案有效,但是,您知道有任何带示例的文档可以解释JSON的这种操作吗?python中的字典操作:感谢您的时间和帮助。请看一下模块。加载后,您可以读取json,在Python目录或列表上进行修改,然后使用转储将其写回。您所需的格式不是有效的json。@OmkarDeshpande此文件是使用API生成的,因此是json文件的输出。我需要这种特定的格式,因为BigQuery只接受这种方式(经过测试),但这种方式可能不正确。
json.load
将无法以所需的格式工作。@OmkarDeshpande我该怎么做才能做到这一点?