Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How tho convert JSON format

avatar

The system produced in the 1 line the keys und in the next lines the values:

[
 [
  "host_state",
  "host",
  "host_icons",
  "num_services_ok",
  "num_services_warn",
  "num_services_unknown",
  "num_services_crit",
  "num_services_pending"
 ],
 [
  "UP",
  "DUSTSADMIN.ads.xyz.de",
  "menu",
  "28",
  "0",
  "0",
  "0",
  "0"
 ],
 [
  "UP",
  "DUSTSVMDC01.ads.xyz.de",
  "menu",
  "34",
  "0",
  "0",
  "0",
  "0"
 ],
 [
  "UP",
  "DUSTSVMDC02.ads.xyz.de",
  "menu",
  "34",
  "0",
  "0",
  "0",
  "0"
 ]
 
How to convert to key=value structure ?:

{
	"host_state":"UP",
	"host":"DUSTSVMDC01.ads.xyz.de",
 	"host_icons":"menu",
  	"num_services_ok":"28",
  	"num_services_warn":"0",
  	"num_services_unknown":"0",
  	"num_services_crit":"0",
  	"num_services_pending":"0",
}
1 ACCEPTED SOLUTION

avatar
Master Guru

You can use the JoltTransformJSON processor for this. Here is a Chain spec that should get you what you want:

[
  {
    "operation": "shift",
    "spec": {
      "0": {
        "*": "header"
      },
      "*": "data[]"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "*": {
            "@0": "[&2].@(4,header[&])"
          }
        }
      }
    }
  }
]

The first operation splits the top array into "header" (containing the first element) and "data" (containing the rest). The second operation creates an array of objects, each object having the keys from the header associated with the values from each element in the "data" array. With your input above, I get the following output:

[ {
  "host" : "DUSTSADMIN.ads.xyz.de",
  "host_icons" : "menu",
  "host_state" : "UP",
  "num_services_crit" : "0",
  "num_services_ok" : "28",
  "num_services_pending" : "0",
  "num_services_unknown" : "0",
  "num_services_warn" : "0"
}, {
  "host" : "DUSTSVMDC01.ads.xyz.de",
  "host_icons" : "menu",
  "host_state" : "UP",
  "num_services_crit" : "0",
  "num_services_ok" : "34",
  "num_services_pending" : "0",
  "num_services_unknown" : "0",
  "num_services_warn" : "0"
}, {
  "host" : "DUSTSVMDC02.ads.xyz.de",
  "host_icons" : "menu",
  "host_state" : "UP",
  "num_services_crit" : "0",
  "num_services_ok" : "34",
  "num_services_pending" : "0",
  "num_services_unknown" : "0",
  "num_services_warn" : "0"
} ]

View solution in original post

2 REPLIES 2

avatar
Master Guru

You can use the JoltTransformJSON processor for this. Here is a Chain spec that should get you what you want:

[
  {
    "operation": "shift",
    "spec": {
      "0": {
        "*": "header"
      },
      "*": "data[]"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "*": {
            "@0": "[&2].@(4,header[&])"
          }
        }
      }
    }
  }
]

The first operation splits the top array into "header" (containing the first element) and "data" (containing the rest). The second operation creates an array of objects, each object having the keys from the header associated with the values from each element in the "data" array. With your input above, I get the following output:

[ {
  "host" : "DUSTSADMIN.ads.xyz.de",
  "host_icons" : "menu",
  "host_state" : "UP",
  "num_services_crit" : "0",
  "num_services_ok" : "28",
  "num_services_pending" : "0",
  "num_services_unknown" : "0",
  "num_services_warn" : "0"
}, {
  "host" : "DUSTSVMDC01.ads.xyz.de",
  "host_icons" : "menu",
  "host_state" : "UP",
  "num_services_crit" : "0",
  "num_services_ok" : "34",
  "num_services_pending" : "0",
  "num_services_unknown" : "0",
  "num_services_warn" : "0"
}, {
  "host" : "DUSTSVMDC02.ads.xyz.de",
  "host_icons" : "menu",
  "host_state" : "UP",
  "num_services_crit" : "0",
  "num_services_ok" : "34",
  "num_services_pending" : "0",
  "num_services_unknown" : "0",
  "num_services_warn" : "0"
} ]

avatar

Hi Matt, great solution!!!

Thanks Timo