JQ 合并 WooCommerce REST API 响应
JQ merge WooCommerce REST API responce
我正在尝试从 linux 控制台解析来自 woocommerce 的数据。
只需要带上运输信息和商品名称以及数量。
我正在向 wp-json/wc/v2/orders/ORDER_ID
发出 curl 请求
然后
jq '{order_id:.id,ship_info:.shipping,items: (.line_items[] | {name , quantity} ) }'
如果订单包含两个项目,jq 将 return 两个对象
示例:
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"name": "Black T-shirt",
"quantity": 1
}
}
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"name": "White T-shirt",
"quantity": 1
}
}
我想要合并项目并使用项目的名称作为键和项目的数量作为值。请建议如何获得这样的输出
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"White T-shirt": "1",
"Black T-shirt": "1"
}
}
对于当前的 jq
查询,您正在迭代 生成对象中的项目。这就是为什么您每件商品都会收到一件物品。与其事后合并它们,不如一开始就不要将它们分开。
如果您从
更改了查询
jq '{
order_id: .id,
ship_info: .shipping,
items: (.line_items[] | {name, quantity})
}'
只是
jq '{
order_id: .id,
ship_info: .shipping,
items: .line_items
}'
您可能已经看到 .line_items
实际上是一个数组。
要根据您想要的输出对其进行转换,请将该行更改为以下内容之一。它们都应该产生相同的结果。
items: (.line_items | map({(.name): .quantity}) | add)
items: (INDEX(.line_items[]; .name) | map_values(.quantity))
items: (reduce .line_items[] as $i ({}; .[$i.name] = .quantity))
我正在尝试从 linux 控制台解析来自 woocommerce 的数据。 只需要带上运输信息和商品名称以及数量。
我正在向 wp-json/wc/v2/orders/ORDER_ID
发出 curl 请求
然后
jq '{order_id:.id,ship_info:.shipping,items: (.line_items[] | {name , quantity} ) }'
如果订单包含两个项目,jq 将 return 两个对象
示例:
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"name": "Black T-shirt",
"quantity": 1
}
}
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"name": "White T-shirt",
"quantity": 1
}
}
我想要合并项目并使用项目的名称作为键和项目的数量作为值。请建议如何获得这样的输出
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"White T-shirt": "1",
"Black T-shirt": "1"
}
}
对于当前的 jq
查询,您正在迭代 生成对象中的项目。这就是为什么您每件商品都会收到一件物品。与其事后合并它们,不如一开始就不要将它们分开。
如果您从
更改了查询jq '{
order_id: .id,
ship_info: .shipping,
items: (.line_items[] | {name, quantity})
}'
只是
jq '{
order_id: .id,
ship_info: .shipping,
items: .line_items
}'
您可能已经看到 .line_items
实际上是一个数组。
要根据您想要的输出对其进行转换,请将该行更改为以下内容之一。它们都应该产生相同的结果。
items: (.line_items | map({(.name): .quantity}) | add)
items: (INDEX(.line_items[]; .name) | map_values(.quantity))
items: (reduce .line_items[] as $i ({}; .[$i.name] = .quantity))