0

I am trying to convert the input JSON

{
  "Main header": {
    "line1": [
      {
        "model": "Cooper",
        "year": 2018,
        "type": "Hatchback",
        "motorization": "Electric",
        "colour": "Midnight Black",
        "stageID": "MGOP94810482042"
      }
    ],
    "line2": [
      {
        "model": "Cooper",
        "year": 2018,
        "type": "Hatchback",
        "motorization": "Diesel",
        "colour": "Silver",
        "stageID": "MGOP9183740194"
      }
    ]
  }
}

to output

{
  "My header": {
    "This is line 1": [
      {
        "Car Model": "Cooper",
        "Car Year": 2018,
        "type": "Hatchback",
        "motorization": "Electric",
        "colour": "Midnight Black",
        "stageID": "MGOP94810482042"
      }
    ],
    "This is line 2": [
      {
        "model": "Cooper",
        "year": 2018,
        "type": "Hatchback",
        "motorization": "Diesel",
        "colour": "Silver",
        "stageID": "MGOP9183740194"
      }
    ]
  }
}

I can perform this in multiple different steps.

  1. Rename line1 and line 2 headers
[
  {
    "operation": "shift",
    "spec": {
      "Main header": {
        "*": {
          "@": "&"
        },
        "line1": {
          "@": "Line 1 header"
        },
        "line2": {
          "@": "Line 2 header"
        }
      }
    }
  }
  ]
  1. Rename specific keys in a array
 [
   {
     "operation": "shift",
     "spec": {
       "Main header": {
         "line1": {
           "*": {
             "model": "Main header.line1.[&1].Car Model",
             "year": "Main header.line1.[&1].Car Year"
           }
         }
       }
     }
  }
 ]

I am trying to create a combined jolt transform spec that can perform this conversion. I tried creating a couple of merged versions and one or the other doesn't seem to work. Any help or guidance is much appreciated!

EDIT

I managed to club the two operations and arrived at the below spec

 //Rename nested keys in array
 [
   {
     "operation": "shift",
     "spec": {
       "Main header": {
         "line1": {
           "*": {
             "model": "My Header.This is line 1.[&1].Car Model",
             "year": "My Header.This is line 1.[&1].Car Year",
             "@": "My Header.This is line 1.[&]"
           }
         },
         "line2": {
           "*": {
             "model": "My Header.This is line 2.[&1].Car Model",
             "year": "My Header.This is line 2.[&1].Car Year",
             "@": "My Header.This is line 2.[&]"
           }
         }
       }
     }
   }
 ]

But when I run this spec, there are two new keys (the ones I renamed) and all the existing keys are also present in the output. Is there a way I can remove the keys that I have renamed?

My latest output

{
  "My Header" : {
    "This is line 1" : [ {
      "model" : "Cooper",
      "year" : 2018,
      "type" : "Hatchback",
      "motorization" : "Electric",
      "colour" : "Midnight Black",
      "stageID" : "MGOP94810482042",
      "Car Model" : "Cooper",
      "Car Year" : 2018
    } ],
    "This is line 2" : [ {
      "model" : "Cooper",
      "year" : 2018,
      "type" : "Hatchback",
      "motorization" : "Diesel",
      "colour" : "Silver",
      "stageID" : "MGOP9183740194",
      "Car Model" : "Cooper",
      "Car Year" : 2018
    } ]
  }
}

1 Answer 1

1

Suggestion 1

Using remove operation

Append the spec at the end, removes the elements mentioned

[
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "*": {
            "model": "",
            "year": ""
          }
        }
      }
    }
  }
]

Suggestion 2

Shifting the each values instead of using @

"type": "My Header.This is line 1.[&1].type",
"motorization": "My Header.This is line 1.[&1].motorization",
"colour": "My Header.This is line 1.[&1].colour",
"stageID": "My Header.This is line 1.[&1].stageID"
Sign up to request clarification or add additional context in comments.

4 Comments

Great. This works. However, is there a way I can shift all but two updated keys without using the remove operation?
Then you can go for Suggestion 2, Specifying the node names in the shift spec instead of @
This was just a sample JSON having similar structure to that of mine. There are 100's of keys my actual file and they are dynamic, so I cannot hard code each of them. The case was to convert only specific ones and retain the rest as-is.
Then. its better to prefer remove operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.