Support Questions

Find answers, ask questions, and share your expertise

Update json value based on 2 other values if it is eqlues to flow file attrpute

avatar
Explorer

Hi 

I have the next JSON

 

{ "model": "2002",
  "cars_drivers" : [ {
    "id" : "1",
    "name" : "Mick",
    "is_processed" : 0
  }, {
    "id" : "2",
    "model" : "John",
    "is_processed" : 0
  }}

 

and I have 2 attributes
attr_id =  1
attr_name = Mick

 

I want to update is_processed in cars_drivers to 1 if the id = attr_id  and name = attr_name

 

So the expected result will be 

 

 

{ "model": "2002",
  "cars_drivers" : [ {
    "id" : "1",
    "name" : "Mick",
    "is_processed" : 1
  }, {
    "id" : "2",
    "model" : "John",
    "is_processed" : 0
  }}

 

 

1 REPLY 1

avatar
Super Guru

Hi,

I'm only able to do that via two processors:

1- JotlTransfomJSON : this is to add desired attributes (attr_id & attr_name) to the flowfile json using the following spec:

 

[
  {
    "operation": "shift",
    "spec": {
	  "#${attr_id}":"attr_id",
	  "#${attr_name}":"attr_name",
      "model": "model",
      "cars_drivers": {
        "*": {
          "id": "cars_drivers[#2].id",
          "name": "cars_drivers[#2].name",
          "is_processed": "cars_drivers[#2].is_processed"
        }
      }
    }
  }
]

 

2 - UpdateRecord Processor: Once the attributes are added to the Json , you can update those records Is_processed value only when the id matches the attr_id and name matches the attr_name. To do that set the following properties:

     a- Replacement Value Strategy: Literal Value

     b- /cars_drivers[*]/id[.=../../../attr_id]/../name[.=../../../attr_name]/../is_processed : 1

 

SAMSAL_0-1667940236704.png

Hope that helps, if it does please accept solution.