- Subscribe to RSS Feed
- Mark Question as New
- Mark Question as Read
- Float this Question for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
Avro to Json adding extra delemeters
- Labels:
-
Apache NiFi
Created on ‎12-11-2023 07:14 AM - edited ‎12-11-2023 07:15 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am using ExecuteSQL to pull a colb column from DB. ExecuteSQL provides the result in the avro format. Then I am converting that to Json using AvroToJsonProcessor but it is adding escape characters.
I want the value that is being pulled from DB as a Json but I am getting it as a string with escape characters.
I want that value of Search result to be a Json instead of a String so that I can use JOLT to transform it as needed.
Can you someone pls suggest how to do it.
Created ‎12-11-2023 10:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
one thing that worked for me use combination of EvaluateJsonPath -> UpdateAttribute -> ReplaceText
I have added the entire json to attribute and in UpdateAttribute I have used "${searchValue:unescapeJson():replace('"{','{'):replace('"}','}')}" which does the trick and then used ReplaceText to replace the Entire Json content.
Created ‎12-11-2023 11:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Another option is to use the UpdateRecod with an Avro schema in the JsonRecordSetWriter that reflects the actual json structure coming out of the ExecuteSQL
The UpdateRecord will look like this:
The JsonRecorSetWriter looks like this:
The AvroSchema provided in the ShemaText property is the following:
{
"name": "nifi",
"type": "record",
"namespace": "nifi.com",
"fields": [
{
"name": "SEARCH_RESULT",
"type": {
"name": "SEARCH_RESULT",
"type": "record",
"fields": [
{
"name": "requestId",
"type": "string"
},
{
"name": "responseData",
"type": {
"name": "responseData",
"type": "array",
"items": {
"name": "responseData",
"type": "record",
"fields": [
{
"name": "searchKey",
"type": "string"
},
{
"name": "data",
"type": {
"name": "data",
"type": "array",
"items": {
"name": "data",
"type": "record",
"fields": [
{
"name": "firstName",
"type": "string"
},
{
"name": "lastName",
"type": "string"
}
]
}
}
}
]
}
}
}
]
}
}
]
}
This will produce the following output json out of the UpdateRecord:
[ {
"SEARCH_RESULT" : {
"requestId" : "203680",
"responseData" : [ {
"searchKey" : "cardNumber",
"data" : [ {
"firstName" : "Martin",
"lastName" : "Garry"
}, {
"firstName" : "Martin",
"lastName" : "Garry"
}, {
"firstName" : "Martin",
"lastName" : "Garry"
} ]
} ]
}
} ]
You can use EvaluateJsonPath to get the data as json array to do the needed processing.
If that helps please accept solution.
Thanks
Created ‎12-11-2023 11:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Another option is to use the UpdateRecod with an Avro schema in the JsonRecordSetWriter that reflects the actual json structure coming out of the ExecuteSQL
The UpdateRecord will look like this:
The JsonRecorSetWriter looks like this:
The AvroSchema provided in the ShemaText property is the following:
{
"name": "nifi",
"type": "record",
"namespace": "nifi.com",
"fields": [
{
"name": "SEARCH_RESULT",
"type": {
"name": "SEARCH_RESULT",
"type": "record",
"fields": [
{
"name": "requestId",
"type": "string"
},
{
"name": "responseData",
"type": {
"name": "responseData",
"type": "array",
"items": {
"name": "responseData",
"type": "record",
"fields": [
{
"name": "searchKey",
"type": "string"
},
{
"name": "data",
"type": {
"name": "data",
"type": "array",
"items": {
"name": "data",
"type": "record",
"fields": [
{
"name": "firstName",
"type": "string"
},
{
"name": "lastName",
"type": "string"
}
]
}
}
}
]
}
}
}
]
}
}
]
}
This will produce the following output json out of the UpdateRecord:
[ {
"SEARCH_RESULT" : {
"requestId" : "203680",
"responseData" : [ {
"searchKey" : "cardNumber",
"data" : [ {
"firstName" : "Martin",
"lastName" : "Garry"
}, {
"firstName" : "Martin",
"lastName" : "Garry"
}, {
"firstName" : "Martin",
"lastName" : "Garry"
} ]
} ]
}
} ]
You can use EvaluateJsonPath to get the data as json array to do the needed processing.
If that helps please accept solution.
Thanks
Created ‎12-12-2023 06:45 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @SAMSAL
when I try in my local with the schema text that you provided.
I am seeing this error:
Input that I am using is: [{
"SEARCH_RESULT": "{ \"requestId\": \"203680\", \"responseData\": [ { \"searchKey\": \"cardNumber\", \"data\": [ { \"firstName\": \"Martin\", \"lastName\": \"Garry\" }, { \"firstName\": \"Martin\", \"lastName\": \"Garry\"}, { \"firstName\": \"Martin\", \"lastName\": \"Garry\", } ] } ] }"
}]
also tried with
{
"SEARCH_RESULT": "{ \"requestId\": \"203680\", \"responseData\": [ { \"searchKey\": \"cardNumber\", \"data\": [ { \"firstName\": \"Martin\", \"lastName\": \"Garry\" }, { \"firstName\": \"Martin\", \"lastName\": \"Garry\"}, { \"firstName\": \"Martin\", \"lastName\": \"Garry\", } ] } ] }"
}
org.apache.nifi.serialization.record.util.DataTypeUtils.toRecord(DataTypeUtils.java:444)
Created ‎12-12-2023 06:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you post screenshot of the UpdateRecord processor configuration? Also you have to be careful with the provided input because there is an extra comma after last Garry value which makes the json invalid.

- « Previous
-
- 1
- 2
- Next »