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

Viewed 72

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": "user@email.com"
}

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": "user@email.com"
}

Any help would be greatly appreciated!

1 Answers

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 http://jolt-demo.appspot.com/ is

enter image description here

Related