【发布时间】:2021-12-05 05:53:59
【问题描述】:
我有一个包含如下数据的文本文件:
6111119268639|22|65024:3|2000225350|Samsung|ADD|234534643645|REMOVE|5645657|65067:3|Apple|ADD|234534643645|REMOVE|3432523|65023:3
6111119268639|22|65024:3|2000225350|Apple|ADD|234534643645|REMOVE|3432523|65023:3
6111119268639|22|65024:3|2000225350|Samsung|ADD|234534643645|REMOVE|3432523|65023:3
等等……
我想要像下面这样的 json 输出:
[{
"ExternalId": "6111119268639",
"ExternalIdType": "22",
"RPPI": "65024:3",
"NewPrimaryOfferId": "2000225350",
"Samsung": [{
"Action": "ADD",
"NewSecondaryOfferId": "234534643645"
},
{
"Action": "REMOVE",
"SecondaryProductOfferId": "5645657",
"RemoveSecondaryProductInstance": "65067:3"
}
],
"Apple": [
{
"Action": "ADD",
"NewComponentOfferId": "234534643645"
},
{
"Action": "REMOVE",
"ComponentOfferId": "3432523",
"RemoveAddOnProductInstance": "65023:3"
}
]
},
{
"ExternalId": "6111119268639",
"ExternalIdType": "22",
"RPPI": "65024:3",
"NewPrimaryOfferId": "2000225350",
"Apple": [{
"Action": "ADD",
"NewComponentOfferId": "234534643645"
},
{
"Action": "REMOVE",
"ComponentOfferId": "3432523",
"RemoveAddOnProductInstance": "65023:3"
}
]
},
{
"ExternalId": "6111119268639",
"ExternalIdType": "22",
"RPPI": "65024:3",
"NewPrimaryOfferId": "2000225350",
"Apple": [{
"Action": "Samsung",
"NewComponentOfferId": "234534643645"
},
{
"Action": "REMOVE",
"ComponentOfferId": "3432523",
"RemoveAddOnProductInstance": "65023:3"
}
]
}
]
这里 ExternalId,ExternalIdType,RPPI,NewPrimaryOfferId 是恒定的,并且会出现在每一行中。但是 Samsung 和 Apple 可能会有所不同,这意味着一行中可能只有“Samsung”,或者一行中可能只有'Apple',或者可能有both,如图所示示例文本。
我为此编写了一个 Jq 命令,如下所示:
jq -Rn '[inputs / "|" | [[
["ExternalId"],["ExternalIdType"],["RPPI"],["NewPrimaryOfferId"],
(("Samsung", "Apple") as $p |
[$p, 0] + (["Action"], ["NewSecondaryOfferId"]),
[$p, 1] + (["Action"], ["SecondaryProductOfferId"], ["RemoveSecondaryProductInstance"])
)
],.] | transpose | reduce .[] as $k ({}; setpath($k[0];$k[1]))]' data.txt
但似乎它没有给我想要的输出。请建议我如何使用产品的 if-else 条件或任何 shell 脚本为此编写 jq 命令以获得所需的 json 输出。提前致谢!
【问题讨论】:
-
使用 perl、python 或您喜欢的任何其他脚本语言可能更容易完成。
-
目前还不清楚,是什么让
"Action": "ADD"之后的下一个字段被称为"NewComponentOfferId"或"NewSecondaryOfferId"。"Action": "REMOVE"之后的字段也是如此:有时是"ComponentOfferId"和"RemoveAddOnProductInstance",有时是"SecondaryProductOfferId"和"RemoveSecondaryProductInstance"。