-1

could anyone help to convert below mention input json to desired output json using jolt ?

Here in treefield list i want to fetch "paramid" and "paramvalue" to first level for only matched "paramid", rest of the list items should be intact in that treefield list.

e.g i want to take paramid "k1" with its value to first level as mentioned in the output.

Input

  {
    "A": "value1",
    "B": "value2",
    "C": {
      "D": "x1",
      "E": {
        "treefield": [
          {
            "paramid": "k1",
            "paramvalue": {
              "string": "value1"
            }
          },
          {
            "paramid": "k2",
            "paramvalue": {
              "string": "value2"
            }
          },
          {
            "paramid": "k3",
            "paramvalue": {
              "string": "value3"
            }
          }       
        ]
      },
      "F": {
        "a": "x1",
        "x": {
          "y": 1
        }
      },
      "H": "x4"
    }
  }
]```


**Output**

```[
  {
    "A": "value1",
    "B": "value2",
    "C": {
      "D": "x1",
      "E": {
        "treefield": [
          {
            "paramid": "k1",
            "paramvalue": {
              "string": "value1"
            }
          },
          {
            "paramid": "k3",
            "paramvalue": {
              "string": "value3"
            }
          }
        ]
      },
      "F": {
        "a": "x1",
        "x": {
          "y": 1
        }
      },
      "H": "x4"
    },
    "k2": "value2"
  }
]```
TJJ
  • 31
  • 1
  • 7

1 Answers1

0

If I understand what you're trying to do (pull out key/value pairs and put them at the top level), this spec should do it:

[
  {
    "operation": "shift",
    "spec": {
      "C": {
        "E": {
          "treefield": {
            "*": {
              "paramvalue": {
                "string": "@(2,paramid)"
              }
            }
          }
        },
        "*": "&"
      },
      "*": "&"
    }
  }
]

That works on individual JSON objects. In your sample input you don't have a leading array bracket but you do have a trailing one, so if you expect multiple objects in an array and want them output in an array, this spec should work:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "C": {
          "E": {
            "treefield": {
              "*": {
                "paramvalue": {
                  "string": "[&6].@(2,paramid)"
                }
              }
            }
          },
          "*": "[&2].&"
        },
        "*": "[&1].&"
      }
    }
  }
]
mattyb
  • 11,693
  • 15
  • 20