Home > Enterprise >  Create JSON array with hardcoded keys and values while using value from key using Jolt
Create JSON array with hardcoded keys and values while using value from key using Jolt

Time:04-29

I have a situation where I am attempting to take some flat json input and provide some contextual information that is needed for API ingest. In the below example, I am trying to add some hardcoded keys into an array.

I've searched quite a bit but haven't seen this specific question asked.

JSON input

{
  "hostname": "computername",
  "user": "DOMAIN\\User",
  "full_name": "lastname firstname",
  "login_name": "DOMAIN\\User",
  "user_email": "[email protected]"
}

Expected outcome:

{
  "entities": [
    {
      "role": "hostname",
      "entities": [
        "computername"
      ]
    },
    {
      "role": "user",
      "entities": [
        "DOMAIN\\User"
      ]
    },
    {
      "role": "full_name",
      "entities": [
        "lastname firstname"
      ]
    },
    {
      "role": "login_name",
      "entities": [
        "DOMAIN\\User"
      ]
    }
  ],
  "user_email": "[email protected]"
}

Any help would be greatly appreciated!

CodePudding user response:

You can use "$" wilcard to derive key names, and "@" wilcard to derive values of the respective attributes nested within a common object representation of a shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "&.role",
        "@": "&.entities[]"
      },
      "user_*": "&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "entities",
      "user_*": "&"
    }
  }
]

the demo on the site enter image description here

  • Related