skip to Main Content

I have a JSON formatted payload with nested objects that I want to modify into an array so Nifi can process the array elements pushing them into SQL. Input looks like this:

[
  {
    "architecture": "x64",
    "hostname": "LAPTOP-B442R49A",
    "memory": "32",
    "network0": {
      "asset_id": 1,
      "defaultgateway": "192.168.1.99",
      "dnsserver": "192.168.1.99",
      "ipaddress": "192.168.1.239"
    },
    "network1": {
      "asset_id": 1,
      "defaultgateway": "",
      "dnsserver": "",
      "ipaddress": "192.168.56.1"
    },
    "volume0": {
      "asset_id": 1,
      "caption": "C:\",
      "freespace": "89"
    },
    "volume1": {
      "asset_id": 1,
      "caption": "D:\",
      "freespace": "0"
    },
    "asset_id": 1
  }
]

Desired output is:

[
  {
    "architecture": "x64",
    "hostname": "LAPTOP-B442R49A",
    "memory": "32",
    "networks": [
      {
        "name": "network0",
        "asset_id": 1,
        "defaultgateway": "192.168.1.99",
        "dnsserver": "192.168.1.99",
        "ipaddress": "192.168.1.239"
      },
      {
        "name": "network1",
        "asset_id": 1,
        "defaultgateway": "",
        "dnsserver": "",
        "ipaddress": "192.168.56.1"
      }
    ],
    "volumes": [
      {
        "name": "volume0",
        "asset_id": 1,
        "caption": "C:\",
        "freespace": "89"
      },
      {
        "name": "volume1",
        "asset_id": 1,
        "caption": "D:\",
        "freespace": "0"
      }
    ],
    "asset_id": 1
  }
]

In the end we came up with the following spec (only for volumes):

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "volume*": {
          "caption": "&.description",
          "freespace": "&.capacity_free",
          "name": "&.drive_name",
          "asset_id": "&.asset_id"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "volumes[]"
    }
  }
]

But that one does not get us what we want. Thanks for any help.

2

Answers


  1. Hope the below Jolt spec will be helpful for you.

    [{
        "operation": "shift",
        "spec": {
          "*": {
            "volume*": {
              "@": "[#3].&1",
              "$": "[#3].&1.name"
            },
            "network*": {
              "@": "[#3].&1",
              "$": "[#3].&1.name"
            },
            "*": "[#].&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "volume*": "[&1].volume[]",
            "network*": "[&1].network[]",
            "*": "[&1].&"
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. You can use separating the literals by asterikses and completing the missing spellings tecnique such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].&", // all elements except for "volume" and "network"
            "*rk*": {
              "$": "[&2].&(1,1)rks.[&(1,2)].name", // &(1,2) represents going 1 level 
              // up the tree and grabbing the second replacement of asterisk
              "*": "[&2].&(1,1)rks.[&(1,2)].&"
            },
            "*me*": {
              "$": "[&2].&(1,1)mes.[&(1,2)].name", //  &(1,2) here matches  ordinal  
              // integers such as 0,1..etc.
              "*": "[&2].&(1,1)mes.[&(1,2)].&"
            }
          }
        }
      }
    ]
    
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search