【问题标题】:I am trying to make a json for the jolt transform but I am stuck in nested array format我正在尝试为 jolt 变换制作一个 json,但我被困在嵌套数组格式中
【发布时间】:2019-10-03 07:24:33
【问题描述】:

我正在尝试转换颠簸,但我在嵌套数组中遇到问题。我没有得到描述值。 我放了一些代码 JSON 输入是

{
  "id": 3,     
  "name": "Sample Product",
  "attribute_set_id": 4,
  "price" : 10,
 "custom_attributes": [
    {
      "attribute_code": "image",
      "value": "/1/_/1.jpg"
    },        
    {
      "attribute_code": "description",
      "value": "<p>This is Sample Product for test</p>"
    }        
  ]  
}

我的 Jolt Spec 是

[
  {
    "operation": "shift",
    "spec": {
      "id": "id",         
      "name": "name",
      "description": "custom_attributes[0].attribute_code.value"
    }
]

我的预期输出是

{
  "id" : 3,
  "name" : "Sample Product",
 "description" : "<p>This is Sample Product for test</p>",
 "image" : "/1/_/1.jpg",
 "start_price" : 10,
 "current_price" : 10
}

【问题讨论】:

    标签: java php arrays jolt


    【解决方案1】:

    如果 "attribute_code": "image" 和 "attribute_code": "description" 总是出现在数组的第一个和第二个元素中,您可以使用以下 Jolt Spec 简单地转换给定的 JSON 字符串:

    [
      {
        "operation": "shift",
        "spec": {
          "id": "id",
          "name": "name",
          "custom_attributes": {
            "1": {
              "value": "description"
            },
            "0": {
              "value": "image"
            }
          },
          "price": ["start_price", "current_price"]
        }
      }
    ]
    

    【讨论】:

    • 但我对 Image 的代码相同,例如 "custom_attributes": { "1": { "value": "image" } } 它抛出错误:重复键
    • @Krunal Pandya 我在Jolt Transform Demo上测试过,没问题!您只需将键从描述重命名为图像。
    • 没有人我想要他们两个,在我的实际代码中有超过 15 个 custom_attributes,我想要其中的 5 个。
    • @Krunal Pandya 我更新了我的帖子。如果您想检索具有特定索引的数组中的更多元素,可以展开它。
    • 我有价格值,我必须将它添加到差异变量中我编辑我的问题请查看它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2019-02-20
    • 2021-10-14
    • 1970-01-01
    • 2021-09-26
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多