JSON to JSON using JOLT Transformation - json

I am new to JOLT and got stuck up this requirement, i saw some examples online but in my requirement i needed to add element in a new structure. I hope anyone will be able to understand what i am trying to say
Input JSON
[
{
"ROWSET": {
"ROW": {
"CLTCORP": "1000", //This is CorpId
"CTLITEM": "5000", //This is CorpItemCd
"WHID": "17", //This is WarehouseId
"CTLFAC": "AAHC", //This is FacilityName
"CORP": "001" //This is CorpItem
}
}
}
]
This is expected JSON
{
"SupplyItemData": {
"CorpId": 1000,
"CorpItemCd": 5000
"Warehouse": [{
"WarehouseId": 17,
"FacilityName": "AAHC"
}]
"CorpItem": 001
}
}
Any help or suggestion is appreciated.
I followed few links Transform JSON-JSON JOLT but could not relate exaclty to my use case

You can use the shift operator to do this. First use the * operator to interate through the root level array. Then inside that, simply map the fields to new field names as follows.
[
{
"operation": "shift",
"spec": {
"*": {
"ROWSET": {
"ROW": {
"CLTCORP": "SupplyItemData.CorpId",
"CTLITEM": "SupplyItemData.CorpItemCd",
"WHID": "SupplyItemData.Warehouse.[0].WarehouseId",
"CTLFAC": "SupplyItemData.Warehouse.[0].FacilityName",
"CORP": "SupplyItemData.CorpItem"
}
}
}
}
}
]

Related

Extract the value from an array in Apache NiFi

I have a JSON with this structure:
{
"documentos": [
{
"valorNota": 229.2,
"tipoDocumento": "PRE_NOTA_FISCAL",
"cnpjEmissorNota": "02130525000177",
"cnpjRemetente": "02130525000177",
"cnpjUnidade": "02130525000177",
"cnpjDestinatario": "14586674000124",
"valorFreteRealizadoNota": 0,
"serie": "1",
"valorFreteCalculadoNota": 1000,
"tipoOperacao": "SAIDA",
"dataEmissao": "2022-12-05 11:03:00",
"numeroNota": 744659
}
],
"dataEmissaoConhecimento": "2022-12-05 11:03:00.0",
"localizacaoDestinoConhecimento": 3002171,
"localizacaoOrigemConhecimento": 3001956,
"cepOrigemConhecimento": 44079006,
"cepDestinoConhecimento": 48880000,
"valorFreteRealizadoConhecimento": 0,
"oidConhecimento": 10802880097,
"valorFreteCalculadoConhecimento": 1000
}
I need to extract the value of "numeroNota" (can be with EvaluateJsonPath or other processor).
How can I do this?
Thanks
One option is to use a shift transformation spec within a JoltTransformJSON processor such as
[
{
"operation": "shift",
"spec": {
"docu*": { //the level of "documentos" array
"*": { //the indexes of the array
"numeroN*": "" //yields the value of the attribute without any wrapper
}
}
}
}
]
which returns 744659 as result for the current case

JSON object to array with JOLT

I have the following JSON object
Input:
{
"17.39.108.85:80": [],
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
and trying to transform it into a list/array as below :
Desired Output:
[
{
"17.39.108.85:80": []
},
{
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
]
What's the best way to use Jolt.
You can use this spec:
Currently, your input is correct, but you need to put each key into an array.
You can get each key in the object and send it to an array with [#2].
Note: #2 is the index of each key. for example: 17.39.108.85:80 is 0 and 10.204.32.9:443 is 1.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "[#2].&"
}
}
}
]

How to move nodes into an array and key into the given node in Nifi?

I have JSON payload like this;
[
{
"Samples":{
"Load":{
"itemId":"bx",
"timestamp":"2019-01-28T16:13:39.387640Z",
"name":null
},
"Press":{
"itemId":"by",
"timestamp":"2019-01-28T16:13:39.387640Z",
"name":null
}
}
}
]
I want receive JSON like below:
{
"Samples":{
"Items":[
{
"tag_name":"Load",
"itemId":"bx",
"timestamp":"2019-01-28T16:13:39.387640Z",
"name":null
},
{
"tag_name":"Press",
"itemId":"by",
"timestamp":"2019-01-28T16:13:39.387640Z",
"name":null
}
]
}
}
How can I do this? Can I use JolTransformRecord? Does this record suit for real-time streaming?
Using very similar problem from GitHub: Could you please assist me ? Moving nodes up into an array, and the key into the nodes you can find out solution. Example which first copies to array the whole object and after that adds tag_name:
[
{
"operation": "shift",
"spec": {
"*": {
"Samples": {
"*": { // keys: Load or Press
// Left hand side "#" means grab the whole object
// that was the right hand side of Load or Press.
// Then send it to Samples.Items array.
"#": "Samples.Items[#2]",
"$": "Samples.Items[#2].tag_name"
}
}
}
}
}
]

Map value to a key using JOLT Spec

Is it possible to map a value to a key using JOLT SPEC.
MY input Json
{
"name": "Rj",
"Age" : "15"
}
Output Json as
{
"Rj" : {
"Age":"15"
}
}
Please provide a Json SPEC for above scenario
Input
{
"name": "Rj",
"Age": "15"
}
Spec
[
{
"operation": "shift",
"spec": {
// #(1,name) -> go up 2 level, come back down and
// grab the value at "name" which is "RJ"
// Thus this evaluates to
// "Age" : "RJ.Age"
"Age": "#(1,name).Age"
}
}
]

Is it possible to match an entire path in Jolt shiftr?

Suppose I want to transform the following
Original
{
"data": {
"a": {
"b": {
"c": {
"value": 1
}
}
}
}
}
For simplicity, say I just want to change value to newValue
Result
{
"data" : {
"a" : {
"b" : {
"c" : {
"newValue" : 1
}
}
}
}
}
I could do that with the following Jolt spec:
Spec
[
{
"operation": "shift",
"spec": {
"data": {
"a": {
"b": {
"c": {
"value": "&4.&3.&2.&1.newValue"
}
}
}
}
}
}
]
But I feel like there should be a less verbose syntax... perhaps something like the following (which does not work):
Desired Syntax... or something like it
[
{
"operation": "shift",
"spec": {
"data.a.b.c.value": "data.a.b.c.newValue" // Even nicer to use & somehow
}
}
]
Is there any Jolt shiftr functionality that I'm missing that would make this nicer?
There is not an existing transform that does that.
Couple of issues:
1) the transform you desire is more of a "nudge" than a "shift". In that you want to change a single value in the Json tree and leave the rest of the tree alone. That doesn't exist in Jolt.
"shift" makes a new output map, and "copies" data from the input to the output.
I tried making shift write to the same input Json tree structure, but it is "dangerous" as it can easily run into ConcurrentModification exceptions.
2) The transform style you have laid out is nice for simple things, but I don't know how it would scale to all the complex stuff wildcard logic (*, [], &).
It is the answer
{
"operation": "modify-overwrite-beta",
"spec": {
"jobInput": {
"ai": {
"common": {
"seed": "#(4,ai.model_structure.seed)"
}
}
}
}
}
#(4,ai.model_structure.seed)
upper sentence means backward 4step on json tree and use seed with this path(ai.model_structure.seed).