Python 如何提取嵌套JSON的所有元素?
我正在尝试将以下JSON代码转换为csv文件,并通过python解析该文件。 我不知道如何超越“变体”部分 我必须阅读各种数组列表,但大多数属性都是可选的 我想看看如何有效地读取每个列表下的每个属性,以便将它们作为逗号分隔的记录写入输出文件,作为ETL处理负载的输入 这是我的代码:Python 如何提取嵌套JSON的所有元素?,python,json,csv,json-normalize,Python,Json,Csv,Json Normalize,我正在尝试将以下JSON代码转换为csv文件,并通过python解析该文件。 我不知道如何超越“变体”部分 我必须阅读各种数组列表,但大多数属性都是可选的 我想看看如何有效地读取每个列表下的每个属性,以便将它们作为逗号分隔的记录写入输出文件,作为ETL处理负载的输入 这是我的代码: import json with open(rb'E:\\ETL\\JaneM\\Test\\JaneM-PFFormattedTest.json', encoding="utf8") as pf
import json
with open(rb'E:\\ETL\\JaneM\\Test\\JaneM-PFFormattedTest.json', encoding="utf8") as pf:
data = json.load(pf)
for p in data["products"]:
print(p['title'])
print(p['admin_graphql_api_id'])
for v in p["variants"]:
print(v['id'])
#for v in p{"Variants"}:
# print(v{product_id})
下面给出的快照是一个JSON输入文件:
{
“产品”:[
{
“id”:1560417337456,
“标题”:“拥抱美好”一行,
“body_html”:“\u003cp\u003e用我们的“拥抱美好”一行字,让你的一天充满活力。将此标志放在架子或照片架上,以鼓励你每天拥抱美好的生活。这套两件式套装包括一个单独的丙烯酸切口,放在木块上。\u003c\/p\u003e”,
“供应商”:“Julia Market”,
“产品类型”:“口音”,
“创建时间”:“2018-11-01T14:11:23-05:00”,
“处理”:“拥抱好的一行”,
更新日期:“2020-07-02T09:17:37-05:00”,
“发布时间”:“2019-09-27T14:17:37-05:00”,
“模板后缀”:“”,
“已发布范围”:“全球范围”,
“标签”:“口音、所有产品、bs-sale-2020、customtext2、礼品、朋友礼品、主菜单、netsuite-shipping\u类别标准、办公室、oos但未停产、驶入夏季、9月销售、signs-decor-promo-2020、2019年春季、标准折扣系列”,
“管理员图形api id”:“gid:\/\/shopify\/Product\/1560417337456”,
“变体”:[
{
“id”:15313179869296,
“产品id”:1560417337456,
“标题”:“默认标题”,
“价格”:“15.00”,
“sku”:“ONELINER-Enclose”,
"立场":一,,
“库存策略”:“拒绝”,
“按价格比较”:空,
“履行服务”:“手册”,
“库存管理”:“shopify”,
“选项1”:“默认标题”,
“选项2”:空,
“选项3”:空,
“创建时间”:“2018-11-01T14:11:23-05:00”,
更新日期:“2020-07-02T09:17:37-05:00”,
“应税”:正确,
“条形码”:“112019151733”,
“克”:181,
“图像id”:空,
“重量”:0.399,
“重量单位”:“磅”,
“库存项目id”:15437614448752,
“库存数量”:51,
“旧库存量”:51,
“税务代码”:“税务代码”,
“需要运输”:正确,
“管理员图形api id”:“gid:\/\/shopify\/ProductVariant\/15313179869296”
}
],
“选择”:[
{
“id”:2125698269296,
“产品id”:1560417337456,
“姓名”:“职务”,
"立场":一,,
“价值观”:[
“默认标题”
]
}
],
“图像”:[
{
“id”:4863201345587,
“产品id”:1560417337456,
"立场":一,,
“创建时间”:“2019-01-18T09:59:41-06:00”,
“更新时间”:“2019-01-18T10:00:28-06:00”,
“alt”:“丙烯酸报价牌,带木制支架,上面写着“拥抱美好”,
“宽度”:2048,
“高度”:2048,
“src”:“https:\/\/cdn.shopify.com\/s\/files\/1\/0207\/8508\/products\/拥抱好的一行一行拥抱。jpg?v=1547827228”,
“变体_id”:[
],
“管理员图形api id”:“gid:\/\/shopify\/ProductImage\/4863201345587”
},
{
“id”:4863216975923,
“产品id”:1560417337456,
"立场":二,,
“创建时间”:“2019-01-18T10:09:15-06:00”,
“更新时间”:“2019-01-18T10:09:23-06:00”,
“alt”:“丙烯酸和木材报价标志”,
“宽度”:2048,
“高度”:2048,
“src:“https:\/\/cdn.shopify.com\/s\/files\/1\/0207\/8508\/products\/acrylic and wood one liner.jpg?v=1547827763”,
“变体_id”:[
],
“管理员图形api id”:“gid:\/\/shopify\/ProductImage\/4863216975923”
}
],
“图像”:{
“id”:4863201345587,
“产品id”:1560417337456,
"立场":一,,
“创建时间”:“2019-01-18T09:59:41-06:00”,
“更新时间”:“2019-01-18T10:00:28-06:00”,
“alt”:“丙烯酸报价牌,带木制支架,上面写着“拥抱美好”,
“宽度”:2048,
“高度”:2048,
“src”:“https:\/\/cdn.shopify.com\/s\/files\/1\/0207\/8508\/products\/拥抱好的一行一行拥抱。jpg?v=1547827228”,
“变体_id”:[
],
“管理员图形api id”:“gid:\/\/shopify\/ProductImage\/4863201345587”
}
},
{
“id”:1560417370224,
“标题”:“勇敢一点”,
“body_html”:“\u003cp\u003e用我们的“勇敢”一行字,鼓起你自己或你所爱的人克服恐惧。这两件套包括一个单独的压克力剪纸,上面写着“勇敢”一词,放在一个木块上。在书架、书桌或照片架上设计这个标志,以供日常鼓励。\u003c\/p\u003e”,
“供应商”:“木兰市场”,
“产品类型”:“口音”,
“创建时间”:“2018-11-01T14:11:23-05:00”,
“把手”:“勇敢一点”,
更新日期:“2020-07-02T08:57:29-05:00”,
“发布时间”:“2019-09-27T14:17:37-05:00”,
“模板后缀”:“”,
“已发布范围”:“全球范围”,
“标签”:“accents,所有产品,bs-sale-2020,customtext2,madeinUSA,netsuite-shipping_类别标准,办公室
def flatten_json(nested_json: dict, exclude: list=[''], sep: str='_') -> dict:
"""
Flatten a list of nested dicts.
"""
out = dict()
def flatten(x: (list, dict, str), name: str='', exclude=exclude):
if type(x) is dict:
for a in x:
if a not in exclude:
flatten(x[a], f'{name}{a}{sep}')
elif type(x) is list:
i = 0
for a in x:
flatten(a, f'{name}{i}{sep}')
i += 1
else:
out[name[:-1]] = x
flatten(nested_json)
return out
df = pd.DataFrame([flatten_json(x) for x in data['products']])
id title body_html vendor product_type created_at handle updated_at published_at template_suffix published_scope tags admin_graphql_api_id image.id image.product_id image.position image.created_at image.updated_at image.alt image.width image.height image.src image.variant_ids image.admin_graphql_api_id id product_id title price sku position inventory_policy compare_at_price fulfillment_service inventory_management option1 option2 option3 created_at updated_at taxable barcode grams image_id weight weight_unit inventory_item_id inventory_quantity old_inventory_quantity tax_code requires_shipping admin_graphql_api_id id product_id name position values id product_id position created_at updated_at alt width height src variant_ids admin_graphql_api_id
0 abab6332761412 "Aww Scks" Greeting Card (INACTIVE) AH Books + Paper 2016-07-29T09:07:36-05:00 aww-shucks-greeting-card 2020-06-08T00:05:37-05:00 None web lsob-10, standard discount collection gid:\/\/verify\/Product\/ab6332761412 14530688388 ab6332761412 1 2016-07-29T09:07:38-05:00 2016-07-29T09:07:38-05:00 None 140 180 https:\/\/cdn.verify.com\/s\/files\/1\/0207\/8508\/products\/Screenshot_2016-07-28_16.24.39.png?v=1469801258 NaN gid:\/\/verify\/ProductImage\/14530688388 21828312900 ab6332761412 Default Title 4.50 BD1615 (INACTIVE) 1 deny None manual verify Default Title None None 2016-07-29T09:07:36-05:00 2020-03-10T15:11:49-05:00 True None 0 None 0.000 g 16074523204 0 0 True gid:\/\/verify\/ProductVariant\/21828312900 7615401988 ab6332761412 Title 1 Default Title 14530688388 ab6332761412 1 2016-07-29T09:07:38-05:00 2016-07-29T09:07:38-05:00 None 140 180 https:\/\/cdn.verify.com\/s\/files\/1\/0207\/8508\/products\/Screenshot_2016-07-28_16.24.39.png?v=1469801258 NaN gid:\/\/verify\/ProductImage\/14530688388
1 1560417370224 "Be Brave" One Liner <p>Encourage yourself or a loved one to move past fear with our “Be Brave” One Liner. This two-piece set includes a single acrylic cutout of the phrase “Be Brave” that rests in a wooden block. Style this sign on a shelf, desk or photo ledge for daily encouragement.<\/p> Jane M Market Accents 2018-11-01T14:11:23-05:00 be-brave-one-liner 2020-07-02T08:57:29-05:00 2019-09-27T14:17:37-05:00 global accents, all-products, bs-sale-2020, customtext2, madeinUSA, netsuite-shipping_category-standard, Office, oos but not discontinued, sail-into-summer, September-sale, signs-decor-promo-2020, spring2019, standard discount collection gid:\/\/verify\/Product\/1560417370224 4863203147827 1560417370224 1 2019-01-18T10:01:42-06:00 2019-01-18T10:01:56-06:00 acrylic quote sign with wooden stand that says "be brave" 2048 2048 https:\/\/cdn.verify.com\/s\/files\/1\/0207\/8508\/products\/be-brave-one-liner-ONELINER-BEBRAVE.jpg?v=1547827316 NaN gid:\/\/verify\/ProductImage\/4863203147827 15313179934832 1560417370224 Default Title 15.00 ONELINER-BEBRAVE 1 deny None manual verify Default Title None None 2018-11-01T14:11:23-05:00 2020-07-02T08:57:29-05:00 True 112019151734 181 None 0.399 lb 15437614514288 287 287 True gid:\/\/verify\/ProductVariant\/15313179934832 2125698302064 1560417370224 Title 1 Default Title 4863203147827 1560417370224 1 2019-01-18T10:01:42-06:00 2019-01-18T10:01:56-06:00 acrylic quote sign with wooden stand that says "be brave" 2048 2048 https:\/\/cdn.verify.com\/s\/files\/1\/0207\/8508\/products\/be-brave-one-liner-ONELINER-BEBRAVE.jpg?v=1547827316 NaN gid:\/\/verify\/ProductImage\/4863203147827
2 1560417370224 "Be Brave" One Liner <p>Encourage yourself or a loved one to move past fear with our “Be Brave” One Liner. This two-piece set includes a single acrylic cutout of the phrase “Be Brave” that rests in a wooden block. Style this sign on a shelf, desk or photo ledge for daily encouragement.<\/p> Jane M Market Accents 2018-11-01T14:11:23-05:00 be-brave-one-liner 2020-07-02T08:57:29-05:00 2019-09-27T14:17:37-05:00 global accents, all-products, bs-sale-2020, customtext2, madeinUSA, netsuite-shipping_category-standard, Office, oos but not discontinued, sail-into-summer, September-sale, signs-decor-promo-2020, spring2019, standard discount collection gid:\/\/verify\/Product\/1560417370224 4863203147827 1560417370224 1 2019-01-18T10:01:42-06:00 2019-01-18T10:01:56-06:00 acrylic quote sign with wooden stand that says "be brave" 2048 2048 https:\/\/cdn.verify.com\/s\/files\/1\/0207\/8508\/products\/be-brave-one-liner-ONELINER-BEBRAVE.jpg?v=1547827316 NaN gid:\/\/verify\/ProductImage\/4863203147827 15313179934832 1560417370224 Default Title 15.00 ONELINER-BEBRAVE 1 deny None manual verify Default Title None None 2018-11-01T14:11:23-05:00 2020-07-02T08:57:29-05:00 True 112019151734 181 None 0.399 lb 15437614514288 287 287 True gid:\/\/verify\/ProductVariant\/15313179934832 2125698302064 1560417370224 Title 1 Default Title 4863214190643 1560417370224 2 2019-01-18T10:08:11-06:00 2019-01-18T10:08:19-06:00 acrylic and wood quote sign 2048 2048 https:\/\/cdn.verify.com\/s\/files\/1\/0207\/8508\/products\/avrylic-and-wood-one-liner_706f5afb-14ef-4de7-b059-94db84baf12c.jpg?v=1547827699 NaN gid:\/\/verify\/ProductImage\/4863214190643