Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

JOLT Transformation to Extract Multiple Values from a String inside a Key

avatar
New Contributor

I have the following JSON:

[ {
  "systemId" : "101.14.311.123",
  "enterpriseId" : "64f54773-5d28-401c-bf48-ffs61ef7b1ec",
  "bootstrapStatus" : "NOTIFICATION_APP_REQ_SENT",
  "name" : "madisfx",
  "site" : "SSS00001230915",
  "redundantGroupId" : "",
  "sky" : "",
  "productName" : "",
  "serialNumber" : "",
  "nsgVersion" : "",
  "id" : "0848edb4-45a2-4bec-a559-14fb4324a0c2",
  "csv" : "MapRecord[{equipoId=madisfx, CIF=A21258120, nsg=NSG_1c4b49e6-005e-44e9-97ea-940b85b61534, organizacionId=64f54773-5d28-401c-bf48-ffs61ef7b1ec, factorForma=3ABC88868AA, sedeId=SSS00001230915, sede=SSS00001230915, equipo=madisfx, organizacion=TEST_ORG.SA}]"
} ]

I want to extract the key-value pairs inside "csv" and build a JSON like such:

[ {
  "systemId" : "101.14.311.123",
  "enterpriseId" : "64f54773-5d28-401c-bf48-ffs61ef7b1ec",
  "bootstrapStatus" : "NOTIFICATION_APP_REQ_SENT",
  "name" : "madisfx",
  "site" : "SSS00001230915",
  "redundantGroupId" : "",
  "sky" : "",
  "productName" : "",
  "serialNumber" : "",
  "nsgVersion" : "",
  "id" : "0848edb4-45a2-4bec-a559-14fb4324a0c2",
  "equipoId" : "madisfx", 
  "CIF" : "A21258120", 
  "nsg" : "NSG_1c4b49e6-005e-44e9-97ea-940b85b61534", 
  "organizacionId" : "64f54773-5d28-401c-bf48-ffs61ef7b1ec", 
  "factorForma" : "3ABC88868AA", 
  "sedeId" : "SSS00001230915", 
  "sede" : "SSS00001230915", 
  "equipo" : "madisfx",
  "organizacion" : "TEST_ORG.SA"
} ]

How can I do this with a JOLT Transformation?

Thanks!

1 REPLY 1

avatar
Expert Contributor

Two solutions:
Before that, I believe, you constructed that(csv) value using groovy/any script. If so, try understanding the following:

inputString = '{"firstName":"John","lastName":"Legend"}'


new JsonSlurper().parseText(inputString)
will return MapRecord[{firstName=John, lastName=Legend}].

So, before writing the flowFile, use new JsonOutput().toJson(new JsonSlurper().parseText(inputString)).

This will give {"firstName":"John","lastName":"Legend"} in Json Format(not String)


If that wasn't the case, try replacing 'MapRecord[{' and '}]' with empty string and split the text with "," as delimiter and for each, split again with "=" as delimiter.