Skip to main content
MarioFRS
Gold ⭐️
July 5, 2022
Solved

Apache Nifi add json array name

Hello everyone,

 

i need your help. I am relatively new to nifi and need your help. I am using the processor ConsumeKafkaRecord_2_6 1.15.3 with the record writer json set record writer. The output is then an array without object name. Which processor or setup can I use to add an array name to the json output? I get this Output 

 

[ {
"moving_id" : 17078440,
"store_id" : 9,
"gtin" : 4064813288877,
"qty" : -1.0,
"condition_indicator" : 40,
"issue_date" : 1656585240000,
"received_date" : 1656547200000,
"update_date" : 1656578057133
}, {
"moving_id" : 17078441,
"store_id" : 9,
"gtin" : 4064813288907,
"qty" : -1.0,
"condition_indicator" : 40,
"issue_date" : 1656585240000,
"received_date" : 1656547200000,
"update_date" : 1656578057133
} ]

 

and i need json file in this structur

 

{"stocks":
[ {
"moving_id" : 17078440,
"store_id" : 9,
"gtin" : 4064813288877,
"qty" : -1.0,
"condition_indicator" : 40,
"issue_date" : 1656585240000,
"received_date" : 1656547200000,
"update_date" : 1656578057133
}, {
"moving_id" : 17078441,
"store_id" : 9,
"gtin" : 4064813288907,
"qty" : -1.0,
"condition_indicator" : 40,
"issue_date" : 1656585240000,
"received_date" : 1656547200000,
"update_date" : 1656578057133
} ]}

 

 

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
July 5, 2022

Hi,

You can use the JoltTransformJson Processor and set the Jolt Specification value to :

 

[
 {
   "operation": "shift",
    "spec": {
           "*": "stocks[]"
           }
 }
]

 

Hope that helps. If it does, Please accept solution. Thanks

4 replies

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
July 5, 2022

Hi,

You can use the JoltTransformJson Processor and set the Jolt Specification value to :

 

[
 {
   "operation": "shift",
    "spec": {
           "*": "stocks[]"
           }
 }
]

 

Hope that helps. If it does, Please accept solution. Thanks

Translate
MarioFRS
MarioFRSAuthor
Gold ⭐️
July 6, 2022

Thank you, this approach works well

Translate
araujo
Platinum ⭐️⭐️⭐️
Platinum ⭐️⭐️⭐️
July 5, 2022

@MarioFRS ,

 

What are you trying to do with the stock records after that step in your flow?

 

Cheers,

André

Translate
-- Was your question answered? Please take some time to click on "Accept as Solution" below this post. If you find a reply useful, say thanks by clicking on the thumbs up button.
MarioFRS
MarioFRSAuthor
Gold ⭐️
July 6, 2022

@araujothe next step is with InvokeHTTP processor to update the stocks of products via Rest Endpoint

Translate