Jolt transform json - how to add default fields
Asked Answered
G

1

6

I have below input JSON:

{
  "id": "2ef8a2ee-054f-4b43-956a-8aa4f51a41d5",
  "type": "VOICE",
  "tags": [
    {
      "id": "some id 1",
      "description": "some description 1"
    },
    {
      "id": "some id 2",
      "description": "some description 2"
    }
  ],
  "transcription": {
    "key1": "val1",
    "key2": "val2"
  }
}

But, output JSON should look like similarly, and add only default values:

{
  "id": "2ef8a2ee-054f-4b43-956a-8aa4f51a41d5",
  "created": "2019-06-18T18:12:37",
  "firstName": "Khusan",
  "lastName": "Sharipov",
  "status": "OPEN"
  "type": "VOICE",
  "tags": [
    {
      "id": "some id 1",
      "description": "some description 1"
    },
    {
      "id": "some id 2",
      "description": "some description 2"
    }
  ],
  "transcription": {
    "key1": "val1",
    "key2": "val2"
  }
}

This is my JOLT spec:

[
  {
    "operation": "shift",
    "spec": {
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "": "TRASH",
        "*": {
          "$": "&2"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "TRASH": ""
    }
  },
  {
    "operation": "default",
    "spec": {
      "firstName": "Khusan",
      "lastName": "Sharipov",
      "status": "OPEN"
    }
  }
]

I should edit JOLT spec but I do not understand how ( default fields first name, last name and status work. created can be added as "created": "@(3,ninjaed-in-time)"

Garay answered 18/6, 2019 at 18:39 Comment(4)
Your output JSON contains one more field: created with date. Your JOLT transformation does something with last name and first name which does not exist in input/output JSON. Could you please clarify what you would like to achieve?Dalton
yeah, output json has created, first name and last name fields. We can add these fields by default in jolt spec. I need output json like as input, but default field should be added in output json(((( sorry I cann't edit my spec as code :(Garay
But could you edit your output JSON ans show how it should look like after transformation?Dalton
sure.. These three default fields I described in jolt spec. But I don't know how I can put sysdate for created field))Garay
C
8

If you want to add only new fields you need to use only default operation like below:

[
  {
    "operation": "default",
    "spec": {
      "firstName": "Khusan",
      "lastName": "Sharipov",
      "status": "OPEN"
    }
  }
]
Cushitic answered 18/6, 2019 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.