1

有没有办法重新加入嵌套字段,因为作为响应,它会分解成单独的对象。

实际反应:

{"id":"gid:\/\/shopify\/Product\/1755556806746","title":"A Book bind","description":"Energize is a pure Sativa formulation designed to stimulate, excite, and keep you productive","productType":"CARTRIDGE","vendor":"ACES","totalInventory":0,"featuredImage":null,"handle":"1g-energizedurban-cart-82-89"}
{"id":"gid:\/\/shopify\/Metafield\/4680431861850","namespace":"custom_fields","key":"thc_percentage","value":"aa","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}
{"id":"gid:\/\/shopify\/Metafield\/4680431894618","namespace":"custom_fields","key":"cbd_percentage","value":"bb","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}
{"id":"gid:\/\/shopify\/Metafield\/4680431960154","namespace":"custom_fields","key":"strain_name","value":"Strain","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}

预期反应:

{"id":"gid:\/\/shopify\/Product\/1755556806746","title":"A Book bind","description":"Energize is a pure Sativa formulation designed to stimulate, excite, and keep you productive","productType":"CARTRIDGE","vendor":"ACES","totalInventory":0,"featuredImage":null,"handle":"1g-energizedurban-cart-82-89",
metafields:[{"id":"gid:\/\/shopify\/Metafield\/4680431861850","namespace":"custom_fields","key":"thc_percentage","value":"aa","__parentId":"gid:\/\/shopify\/Product\/1755556806746"},
{"id":"gid:\/\/shopify\/Metafield\/4680431894618","namespace":"custom_fields","key":"cbd_percentage","value":"bb","__parentId":"gid:\/\/shopify\/Product\/1755556806746"},
{"id":"gid:\/\/shopify\/Metafield\/4680431960154","namespace":"custom_fields","key":"strain_name","value":"Strain","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}
]
}
4

1 回答 1

2

Shopify 使用JSON Lines (JSONL) 数据类型来输出批量操作的响应。

文件中的每一行都是连接中返回的节点对象。如果一个节点有一个嵌套连接,那么每个子节点都被提取到下一行它自己的对象中。

因此,您需要遍历每一行并将其解析为脚本中的 JSON 对象。使用 __parentId属性作为对父对象的引用。
如果需要,将其加入父对象,但您只能在收到响应后执行此操作。您不能强制 Shopify API 为您执行此操作。


有用的链接:

于 2020-05-06T11:06:35.317 回答