将 Shopify JSONL 文件转换为 JSON
Transform Shopify JSONL file to JSON
我有一个 Shopify jsonl 文件,我试图将其映射到不同的 json 结构。
鉴于此输入:
{"id":"gid:\/\/shopify\/Product\/123456789","title":"Gift Card"}
{"originalSrc":"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-01.jpg","__parentId":"gid:\/\/shopify\/Product\/123456789"}
{"originalSrc":"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-02.jpg","__parentId":"gid:\/\/shopify\/Product\/123456789"}
我想要使用 __parentId 将图像映射到正确的产品 ID。
[
{
"id":"gid:\/\/shopify\/Product\/123456789",
"title":"Gift Card",
"images: [
"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-01.jpg",
"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-02.jpg"
]
},
...
]
以下解决方案首先递增地构造字典和具有 id 的对象数组,
然后使用 map
:
产生所需的结果
reduce inputs as $x ({};
if $x|has("id") then .objects += [$x]
elif $x|has("__parentId") then .dict[$x.__parentId] += [$x.originalSrc]
else . # an error?
end)
| .dict as $dict
| .objects
| map( . + {images: $dict[.id]})
调用
必须使用 -n 命令行选项,例如
jq -nf shopify.jq shopify.json
我有一个 Shopify jsonl 文件,我试图将其映射到不同的 json 结构。
鉴于此输入:
{"id":"gid:\/\/shopify\/Product\/123456789","title":"Gift Card"}
{"originalSrc":"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-01.jpg","__parentId":"gid:\/\/shopify\/Product\/123456789"}
{"originalSrc":"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-02.jpg","__parentId":"gid:\/\/shopify\/Product\/123456789"}
我想要使用 __parentId 将图像映射到正确的产品 ID。
[
{
"id":"gid:\/\/shopify\/Product\/123456789",
"title":"Gift Card",
"images: [
"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-01.jpg",
"https:\/\/cdn.shopify.com\/s\/files\/1\/1111\/2222\/products\/gift-card-02.jpg"
]
},
...
]
以下解决方案首先递增地构造字典和具有 id 的对象数组,
然后使用 map
:
reduce inputs as $x ({};
if $x|has("id") then .objects += [$x]
elif $x|has("__parentId") then .dict[$x.__parentId] += [$x.originalSrc]
else . # an error?
end)
| .dict as $dict
| .objects
| map( . + {images: $dict[.id]})
调用
必须使用 -n 命令行选项,例如
jq -nf shopify.jq shopify.json