Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

How tho convert JSON format

Solved Go to solution

How tho convert JSON format

New Contributor

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

Accepted Solutions

Re: How tho convert JSON format

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"
} ]
2 REPLIES 2

Re: How tho convert JSON format

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"
} ]

Re: How tho convert JSON format

New Contributor

Hi Matt, great solution!!!

Thanks Timo

Don't have an account?
Coming from Hortonworks? Activate your account here