从数组到对象的震荡变换并提取值

时间:2019-01-03 08:52:33

标签: json transformation jolt

我需要对以下示例json执行Jolt转换:

输入

Infrastructure as Code

我需要转换为

[
  {
    "DOCUMENT_HEADER_ID": "29120",
    "Descrizione": "GAS",
    "PrezzoTotale": "8.51"
  },
  {
    "DOCUMENT_HEADER_ID": "29120",
    "Descrizione": "IMPOSTE",
    "PrezzoTotale": "7.25"
  },
  ...
]

使用此规范

{
  "DOCUMENT_HEADER_ID" : "29120",
  "invoice" : [ 
  {
    "DOCUMENT_HEADER_ID" : "29120",
    "Descrizione" : "GAS",
    "PrezzoTotale" : "8.51"
  }, {
    "DOCUMENT_HEADER_ID" : "29120",
    "Descrizione" : "IMPOSTE",
    "PrezzoTotale" : "7.25"
  },
  ...
  ]
}

我有这个输出:

[
  {
    "operation": "shift",
    "spec": {
      "@": "invoice"
    }
  }
]

但是我不知道如何从原始数组的第一个元素中提取DOCUMENT_HEADER_ID的值

3 个答案:

答案 0 :(得分:1)

可以单班完成。

[
  {
    "operation": "shift",
    "spec": {
      "@": "invoice",
      "0": {
        "DOCUMENT_HEADER_ID": "DOCUMENT_HEADER_ID"
      }
    }
  }
]

答案 1 :(得分:0)

我找到了正确的规格

[
  {
    "operation": "shift",
    "spec": {
      "@": "invoice"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "invoice": {
        "0": {
          "DOCUMENT_HEADER_ID": "DOCUMENT_HEADER_ID"
        },
        "@": "invoice"
      }
    }
  }
]

答案 2 :(得分:0)

如果必须检索单个元素,则数据如下:

"array_field": ["array_single_element"]

可以使用:

{
"operation": "modify-overwrite-beta",
"spec": {
  "array_field": "=concat(@(1,array_field))",
  "*": "=concat(@(1,&))"
}
}