I am trying to transform a string into an array from a JSON using a JOLT transformation processor in nifi How can I do this?
I am trying to transform a string into an array from
{
"_source": {
"users": {
"attributes": {
"organization": {
"display": "org1",
"objectGUID": "7",
"attributes": {
"distinguishedName": "ou=org1,OU=BU"
}
}
}
}
}
}
and transform it into this
{
"_source": {
"users": {
"attributes": {
"organization": {
"0" : {
"display": "org1",
"objectGUID": "7",
"attributes": {
"distinguishedName": "ou=org1,OU=BU"
}
}
}
}
}
}
}
Sometimes the input json is already transformed into an array and it looks like this
{
"_source": {
"users": {
"attributes": {
"organization": {
"0" : {
"display": "org1",
"objectGUID": "7",
"attributes": {
"distinguishedName": "ou=org1,OU=BU"
}
}
}
}
}
}
}
with the "0" tag added. So in this case, the tag must not be added again. Sometimes I can have multiple elements like this
{
"_source": {
"users": {
"attributes": {
"organization": {
"0" : {
"display": "org1",
"objectGUID": "7",
"attributes": {
"distinguishedName": "ou=org1,OU=BU"
}
},
"1" : {
"display": "org1",
"objectGUID": "7",
"attributes": {
"distinguishedName": "ou=org1,OU=BU"
}
}
}
}
}
}
}
Again this should remain unchanged.
So I want to apply the transformation only if the "0" tag is not there. otherwise leave it as it is.
How can I do this?
You can use such a shift transformation spec which adds an extra object wrapper as desired
[
{
"operation": "shift",
"spec": {
"*": { // level of "_source"
"*": { // level of "users"
"*": { // level of "attributes"
"*": "&3.&2.&1.&.0" // level of "organization"
// identifiers with ampersands copies the values of the outer keys each from their respective levels
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Edit : Based on your last edit and comments you can use the following spec having conditional logic
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"0": "&4.&3.&2.&1.&",
"*": "&4.&3.&2.&1.0.&"
}
}
}
}
}
}
]