Python 将嵌套的 json 表单响应转换为数据帧(多个级别)

Python convert nested json form response to a dataframe (multiples levels)

提问人:Darko37 提问时间:8/22/2023 最后编辑:Darko37 更新时间:8/23/2023 访问量:32

问:

我正在尝试将嵌套的 Json 响应转换为数据帧,但使用 pd.json_normalize(response.json()) 的基本方法生成了 2000 多列......

这是我嵌套的 json 的结构:

"form_items":{
"1218738":{"name":"Autocapture","field_type":"autocapture","options":{"type":"datetime","datetime":"modified_app_utc"},"page_num":"1","page_name":"Section 1","pos":"1"},
"1218739":{"name":"Agent name","field_type":"checkbox","page_num":"1","page_name":"Section 1","pos":"2","items":[{"name":"Agent 1"},{"name":"Agent 2"},{"name":"Agent 3"}]},
"1218740":{"name":"Type","field_type":"radio","page_num":"1","page_name":"Section 1","pos":"3","items":[{"name":"Monthly","opts":"conditional":["1218746"],"default":["1"]}},{"name":"Annual","opts":{"conditional":["1218747"]}}]},
"1218746":{"name":"Works fine ?","field_type":"radio","page_num":"1","page_name":"Section 1","pos":"9","items":[{"name":"Yes"},{"name":"No","opts":{"conditional":["1218747","1218750"]}}]},
"1218747":{"name":"Comment","field_type":"input","options":{"type":"textarea"},"page_num":"1","page_name":"Section 1","pos":"10"},


"data":[
{"event_name":"Event num 1234",
"last_modified":"2023-07-19 13:01:12",
"Event_status":"to be validated",
"Event_id":91458,
"Member_id":50348,
"Member_label":"the guy next door",
"1218739":{"value":"test1","modified":"2023-07-19 13:01:12"},
"1218740":{"value":"Monthly","modified":"2023-07-19 13:01:12"},
"1218741":{"value":"Bad","modified":"2023-07-19 13:01:12"},
"1218743":{"value":"Normal","modified":"2023-07-19 13:01:12"},
"1218746":{"value":"Yes","modified":"2023-07-19 13:01:12"},
"1218751":{"value":"Normal","modified":"2023-07-19 13:01:12"},
"1218747":{"value":"amazing comment","modified":"2023-07-19 13:01:12"},
"subpages":[]
},
{"event_name":"Event num 1235",
"last_modified":"2023-07-18 08:40:27",
"Event_status":"finished",
"Event_id":87692,
"Member_id":90348,
"Member_label":"2d guy next door",
"1218739":{"value":"test16","modified":"2023-07-18 08:40:27"},
"1218740":{"value":"Annual","modified":"2023-07-18 08:40:27"},
"1218741":{"value":"Good","modified":"2023-07-18 08:40:27"},
"1218776":{"value":"Normal","modified":"2023-07-18 08:40:27"},
"subpages":[]},
],
"has_more_pages":true,
"pages":10,
"can_contain_repeatable_section":"0"

有 2 个主键“form_items”和“数据”。

  • “form_items”包含表单的描述,form_items_id如“1218739”,其中包含:{“name”:“Agent name”}和一些可能的项目。

  • “数据”包含详细信息和具有价值的form_items_id。它可以有 30 form_item_id的价值。

“数据”中棘手的部分是form_item_id(“1218738”)不是孤立的,我没有成功提取这部分......

基本上,我只想在数据帧数据部分上放置:

  • ID 为“12xxxxx”的form_items_id
  • 列替换为form_items_id的名称
  • 列替换为

有什么想法吗?

python json 数据帧 rest 嵌套

评论


答: 暂无答案