how to match a word from string in jolt transformation - json

I have start writing a jolt transformation, and getting challenge to match the word from the object value.
input JSON :
{
"application" : "android",
"issue" : "FoundCriticalBug",
}
now if the value in key "issue" is critical then print bug is "critical" else the value may comes like "FoundNetworkBug" or "FoundNoBug"
expected output :
{
"application" : "android",
"issue" : "FoundCriticalBug",
"bug" : "critical"
}
There would be a if-else condition applied but i didn't went further. do suggest.

You can use a hash symbol(#) for the case only when the issue is equal to
FoundCriticalBug(considering the three presented fixed values) in order to generate a line
"bug" : "critical" representing a new attribute as follows
[
{
"operation": "shift",
"spec": {
"*": "&",
"issue": {
"FoundCriticalBug": {
"#1": "&2",
"#critical": "bug"
},
"*": {
"#1": "&2"
}
}
}
}
]

Related

Nifi - Remove zeros(00) of my json in Nifi

Good Night.
I need to remove the zeros of "numero":
{
"cnpjemitente" : "48791685000168",
"numero" : "001262851",
"chavenfe" : "35221148791685000168550030012628511100259840",
"serie" : "3 "
}
The correct column:
{
"cnpjemitente" : "48791685000168",
"numero" : "1262851",
"chavenfe" : "35221148791685000168550030012628511100259840",
"serie" : "3 "
}
I need the leading zeros to be removed whenever possible.
Can anyone help me? Thanks
You can consecutively use the following within a JoltTransformJSON processor ;
toInteger and toString functions in a modify-overwrite-beta transformation spec
and then remove transformation in order to get rid of the auxiliary attribute "numero_"
such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"numero_": "=toInteger(#(1,numero))",// converts to 1262851(leading zeroes removed)
"numero": "=toString(#(1,numero_))"// converts to "1262851"(quoted)
}
},
{
"operation": "remove",
"spec": {
"numero_": ""
}
}
]
or use
toInteger and toString functions in each individual consecutive modify-overwrite-beta transformation spec
such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"numero": "=toInteger"// converts to 1262851
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"numero": "=toString"// converts to "1262851"
}
}
]

Nifi - Replace values

Good Afternoon!
I have a JSON with:
{
"cnpjemitente" : "48791685000168",
"pedido" : "543306",
"pedidocliente" : { },
"emissao" : "20220912"
}
I need to replace the value "pedidocliente: {}" to:
{
"cnpjemitente" : "48791685000168",
"pedido" : "543306",
"pedidocliente" : null,
"emissao" : "20220912"
}
Sometimes the value will come in the field, I just want to send null when it is empty with '{}'.
How can I do it this way?
Thanks!
You can use a modify-overwrite-beta transformation spec within a JoltTransformJSON processor such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"pedidocliente": null
}
}
]
as you only need to change an individual attribute's value without affecting the others.
If it's the case that the value does not return always {}(an empty object), then rather use a shift transformation spec such as
[
{
"operation": "shift",
"spec": {
"pedidocliente": {
"*": "&1.&"
},
"*": "&"
}
}
]

How to delete the text befor the symbol ( _ ) , using jolt json?

i have an input JSON file , which have some attributes those contains some informations which i want to delete on the output
( example input : Hello_World => output = World )
For example this is the input :
{
"test": "hello_world"
}
and this is output needed :
{
"test" : "world"
}
the result derived from the jolt spec i tried is not even close, this is what i tried :
[
{
"operation": "modify-overwrite-beta",
"spec": {
"test": "=test.substring(test.indexOf(_) + 1)"
}
}
]
Sorry im i a newbie at jolt, just started it.
You can split by _ character within a modify transformation spec, and pick the one with the index 1 in order to display the last component of the arrah considering the current case of having a single underscore such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=split('_', #(1,&))"
}
},
{
"operation": "shift",
"spec": {
"*": {
"1": "&1" // &1 represents going one level up to reach to the level of the label "test" to replicate it
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :
Considering an input with attribute having value more than one underscore :
{
"test": "how_are_you"
}
[More generic] solution would be as follows :
[
{
"operation": "modify-overwrite-beta",
"spec": {
"test_": "=split('_', #(1,test))",
"test": "=lastElement(#(1,test_))"
}
},
{
// get rid of the extra generated attribute
"operation": "remove",
"spec": {
"test_": ""
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is :

unable to convert json list to objects using jolt

I need to use jolt transform to do the below JSON transformation.
need to create new columns from the list from reeval column where sometimes we only one value and some times we get multiple values my input data :-
example 1:
{
"id":"1",
"reeval":["one","two"]
}
example 2:
{
"id":"2",
"reeval":["one","two","three"]
}
example 3:
{
"id":"3",
"reeval":["one"]
}
I have written jolt expresson as below
[
{
"operation": "shift",
"spec": {
"id": "id",
"reeval": {
"*": "&"
}
}
}
]
with above jolt expression is working fine but unable to add column name
output for above jolt is as below
example 1:
{
"id" : "1",
"0" : "one",
"1" : "two"
}
example 2:
{
"id" : "2",
"0" : "one",
"1" : "two",
"2" : "three"
}
here i am unable to change the names of the columns as i need to change colunms as below
my expected output after jolt transformation should be like
example 1:
{
"id":"1",
"reeval":"one",
"reeval1":"two"
}
example 2:
{
"id":"2",
"reeval":"one",
"reeval1":"two",
"reeval2":"three"
}
example 3:
{
"id":"3",
"reeval":"one"
}
Prepending &1 to the current ampersand would suffice in order to go one level up the tree, and to grab the key name in the first shift transformation, and then apply another to rename only the key with index zero such as
[
{
"operation": "shift",
"spec": {
"id": "id",
"reeval": {
"*": "&1&"
}
}
},
{
"operation": "shift",
"spec": {
"reeval0": "reeval",
"*": "&"
}
}
]

Break down JSON properties to array of objects

I am trying to transform a simple JSon object into an array of objects with keys and values broken out, but I'm not sure how to quite get there.
I have tried this a number of ways but the closest I got was to create an object with two arrays, instead of an array with multiple objects with two properties each:
EDIT: I am trying to write a spec which would take any object, not this specific object. I do not know what the incoming object will be other than it will have simple properties (values will not be arrays or other objects).
Sample Input:
{
"property": "someValue",
"propertyName" : "anotherValue"
}
Expected Output:
{
"split_attributes": [
{
"key" : "property",
"value": "someValue"
},
{
"key" : "propertyName",
"value" : "anotherValue"
}
]
}
My spec so far:
{
"operation": "shift",
"spec": {
"*": {
"$": "split_attributes[#0].key",
"#": "split_attributes[#0].value"
}
}
}
Produces
{
"split_attributes" : [
{
"key" : [ "property", "propertyName" ],
"value" : [ "someValue", "anotherValue"]
}
]
}
SOLUTION
I was pretty close, and after looking at the tests, the solution was obvious (it's identical to one of the tests)
{
"operation": "shift",
"spec": {
"*": {
"$": "split_attributes[#2].key",
"#": "split_attributes[#2].value"
}
}
}
From what it seems, I was creating an array but I was looking at the wrong level for an index to the new array. I'm still fuzzy on the whole # level (for example where in the "tree" (and of which object) is #0, #1 and #2 actually looking).