Home > Software design >  How to change a name of more fields in Json array using Jolt
How to change a name of more fields in Json array using Jolt

Time:10-16

I am trying to convert the input JSON:

for example , incoming json data structure is

{
  "12346565": [
    {
      "type": "13",
      "value": "3",
      "score": "5"
    },
    {
      "type": "45",
      "value": "1",
      "score": "5"
    }
  ],
  "12346777": [
    {
      "type": "41",
      "value": "10",
      "score": "3"
    }
  ]
}

The expected output is:

{
  "12346565": [
    {
      "model": "13",
      "v": "3"
    },
    {
      "model": "45",
      "v": "1"
    }
  ],
  "12346777": [
    {
      "model": "41",
      "v": "10"
    }
  ]
}
  1. change:
  • type -> model;
  • value -> v
  1. remove:
  • score

Any ideas?

CodePudding user response:

You can use modify and remove transformation specs consecutively such as

[
  {
    // generate new pairs with values copied from the former attributes 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "model": "@(1,type)",
          "v": "@(1,value)"
        }
      }
    }
  },
  {
    // get rid of undesired attributes 
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "type": "",
          "value": "",
          "score": ""
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related