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.

Nifi - How to add key:value to json

Solved Go to solution

Nifi - How to add key:value to json

New Contributor

Hi, we need to add a default key:value with actual timestamp to the following json input structure:

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

needed output:

[ { "ctime" : "Current_timestamp",
  "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"
}, {"ctime" : "Current_timestamp",
  "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"
}, {"ctime" : "Current_timestamp",
  "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"
} ]

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Nifi - How to add key:value to json

As of NiFi 1.2.0, you can use Expression Language in JOLT specifications (NIFI-3010). The following Chain spec adds the ctime field to your input with the current timestamp:

[{
    "operation": "default",
    "spec": {
      "*": {
        "ctime": "${now()}"
      }
    }
}]

If you want ctime you have an integer rather than a string, you can use the following EL expression:

${now():toNumber()}
5 REPLIES 5

Re: Nifi - How to add key:value to json

As of NiFi 1.2.0, you can use Expression Language in JOLT specifications (NIFI-3010). The following Chain spec adds the ctime field to your input with the current timestamp:

[{
    "operation": "default",
    "spec": {
      "*": {
        "ctime": "${now()}"
      }
    }
}]

If you want ctime you have an integer rather than a string, you can use the following EL expression:

${now():toNumber()}

Re: Nifi - How to add key:value to json

New Contributor

Hi Matt,

now i got only this output :

[{

"ctime":"${now()}",

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

},

"ctime":"${now()}",

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

},{"ctime":"${now()}",

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

}]

maybe there ist something wrong with ""?

regards Timo

Re: Nifi - How to add key:value to json

New Contributor

I think my problem my version is : nifi-1.1.2

Highlighted

Re: Nifi - How to add key:value to json

New Contributor

Hi Matt,

after upgrading to nifi 1.2 all works fine !!!

Great work! Thanks

Re: Nifi - How to add key:value to json

New Contributor

Hi Matt,

now i got only this output :

[{

"ctime":"${now()}",

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

},

"ctime":"${now()}",

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

},{"ctime":"${now()}",

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

}]

maybe there ist something wrong with ""?

regards Timo

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