【问题标题】:Text file to json using JQ command使用 JQ 命令将文本文件转换为 json
【发布时间】: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 是恒定的,并且会出现在每一行中。但是 SamsungApple 可能会有所不同,这意味着一行中可能只有“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"

标签: json shell scripting jq


【解决方案1】:

另一种方法:

jq -Rn '
  [
    inputs / "|" | reduce (.[4:] | while(. != [];.[6:])) as $prod (
      .[:4] | with_entries(.key |= ["ExternalId","ExternalIdType","RPPI","NewPrimaryOfferId"][.]);
      .[$prod[0]] = [
        {Action:"ADD", NewComponentOfferId:$prod[2]},
        {Action:"REMOVE", ComponentOfferId:$prod[4], RemoveAddOnProductInstance:$prod[5]}
      ]
    )
  ]
' data.txt

Demo

【讨论】:

    【解决方案2】:

    这似乎适用于您的测试数据:

    jq -nR '
       def offer:
         . as $data |
         [[], 0] | until([$data[.[1]]] | inside(["ADD", "REMOVE"]) | not;
           if $data[.[1]] == "ADD" then
            [ .[0] + [{ Action: "ADD", NewComponentOfferId: $data[.[1] + 1] }], .[1] + 2 ]
           else
            [ .[0] + [{ Action: "REMOVE", ComponentOfferId: $data[.[1] + 1], 
              RemoveAddOnProductInstance: $data[.[1] + 2] }], .[1] + 3 ]
           end);
    
       def build:
           (. / "|") as $data | ($data | length) as $len |
           [ { ExternalId: $data[0], ExternalIdType: $data[1], RPPI: $data[2],
             NewPrimaryOfferId: $data[3] }, 4 ] |
           until(.[1] >= $len;
                 ($data[.[1]+1:] | offer) as $off |
                 [ .[0] + { ($data[.[1]]): $off[0] }, .[1] + 1 + $off[1] ]) |
          .[0];
    
      [ inputs | build ]' data.txt
    

    【讨论】:

    • 当然,模有时会为对象中的键使用不同的名称。
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 2021-11-10
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多