震动转换:使嵌套对象字段成为主Json对象的一部分,并将嵌套对象转换为String

az31mfrm  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(128)

我是JOLT转换的新手,有人能帮我把输入JSON转换成想要的输出吗?

    • 输入JSON:**
[
  {
    "product_id": 1,
    "product_description": "Product 1 details",
    "billing_details": {
      "product_id": 1,
      "billing_description": "Product 1 billing  Details"
    },
    "product_part_desc": {
      "id": 1,
      "part_description": "product 1 part description"
    }
  },
  {
    "product_id": 2,
    "product_description": "Product 2 details",
    "billing_details": {
      "product_id": 1,
      "billing_description": "Product 2 billing  Details"
    },
    "product_part_desc": {
      "id": 1,
      "part_description": "product 2 part description"
    }
  }
]
    • 试用规格:**
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product_id": "[&1].id",
        "product_description": "[&1].description",
        "billing_details": {
          "billing_description": "[&2].bill_desc"
        },
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "product_part_desc": "=toString(@(1,product_part_desc))"
      }
    }
  }
]
    • 预期产出:**
[
  {
    "id": 1,
    "product_description": "Product 1 details",
    "billing_description": "Product 1 billing  Details",
    "product_part_desc": "{id=1, part_description=product 1 part description}"
  },
  {
    "product_id": 2,
    "product_description": "Product 2 details",
    "billing_description": "Product 2 billing  Details",
    "product_part_desc": "{id=1, part_description=product 2 part description}"
  }
]

但上述规格给出了以下输出

ggazkfy8

ggazkfy81#

您可以使用:

      • modify规范中的concat函数,以便获取product_part_desc**属性
      • @billing_details.billing_description标识符以及shift转换规范,以获取billing_description**属性

结果是:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "product_part_desc": "=concat('',@0)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*id": "[&1].id",
        "p*": "[&1].&",
        "@billing_details.billing_description": "[&1].billing_description"
      }
    }
  },
  {// this spec is added only to sort the attributes as desired
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].&",
        "product_description": "[&1].&",
        "billing_description": "[&1].&",
        "product_part_desc": "[&1].&"
      }
    }
  }
]
bvn4nwqk

bvn4nwqk2#

可以使用此等级库:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product_id": "[&1].id",
        "product_description": "[&1].description",
        "billing_details": {
          "billing_description": "[&2].bill_desc"
        },
        "product_part_desc": {
          "*": "[&2].product_part_desc"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "product_part_desc": "=join(', part_description=',@0)",
        "product_part_des*": "=concat('{id=',@0,'}')"
      }
    }
  }
]

您可以在上面的代码中根据需要更改密钥。

相关问题