convert Boolean to String for map values in nifi jolt - json

I want to achieve following JSON transformation using Jolt processor in NIFI
The input in json, is a map (here image is a key and image1.png is a value, and etc, with different types (String, Boolean)
input JSON
{
"internal_value": "434252345",
"settings": {
"image": "image1.png",
"bold": false,
"country": false
}
}
Output JSON should be
{
"internal_value": "434252345",
"settings": {
"image": "image1.png",
"bold": "false",
"country": "false"
}
}
Is there a way to do this using existing Jolt operations ?
Thanks.

As a pure JOLT this would be:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"settings": {
"bold": "=toString",
"country": "=toString"
}
}
}
]
You can use this tool to prototype JOLTs:
https://jolt-demo.appspot.com/#inception
Resources:
JOLT transformation for nested JSON?
JOLT change string to float

Related

Azure CI/CD Json transformation on array

When I'm creating a Task for an Azure DevOps release, can I transform the value of an object in an array, based on the "key" of the object?
As an example, I'm copying the example Json file from this article, which is this.
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=(LocalDb)\\MSDB;AttachDbFilename=aspcore-local.mdf;"
},
"DebugMode": "enabled",
"DBAccess": {
"Administrators": ["Admin-1", "Admin-2"],
"Users": ["Vendor-1", "vendor-3"]
},
"FeatureFlags": {
"Preview": [
{
"newUI": "AllAccounts"
},
{
"NewWelcomeMessage": "Newusers"
}
]
}
}
}
What I have is something more like this, look at the "FeatureFlags" array. My array contains a list of "key"/"value" objects similar to this. I need to be able to transform the object in the array that matches a key, and have the transform process replace the "value" property value.
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=(LocalDb)\\MSDB;AttachDbFilename=aspcore-local.mdf;"
},
"DebugMode": "enabled",
"DBAccess": {
"Administrators": ["Admin-1", "Admin-2"],
"Users": ["Vendor-1", "vendor-3"]
},
"FeatureFlags": {
"Preview": [
{
"key": "newUI",
"value": "AllAccounts"
},
{
"key": "NewWelcomeMessage",
"value": "Newusers"
}
]
}
}
}
For example, I want to change the value of "Newusers" in the object with a key of NewWelcomeMessage, to "AllUsers".
In the Azure Release "Variables" tab, can I simply use this as the "Name", Data.FeatureFlags.Preview.NewWelcomeMessage, and AllUsers as the value for it to transform the value for the correct object, or will this fail?
This pattern seems to work with XML, see this.

JSON attribute value split by space and put them into new attributes using Jolt transform Apache nifi

I have json object as following,
{
"sensorId":2,
"dataValue":26.7,
"dateTime":"2020:12:29 14:20:31"
}
I want to convert it to the following,
{
"sensorId":2,
"dataValue":26.7,
"date":"2020:12:29",
"time":"14:20:31"
}
Using Apache nifi Jolt transform
you can split with space ("* *") and assign splitted parts.
[
{
"operation": "shift",
"spec": {
"sensorId": "sensorId",
"dataValue": "dataValue",
"dateTime": {
"* *": {
"$(0,1)": "date",
"$(0,2)": "time"
}
}
}
}
]

Apache Nifi transform with JoltTranform processor

I have some attributes myId, count. Now with these attributes I want write down the Jolt format to get the following output.
{
"projectId": "projectId",
"ticketId": "NO_TICKET",
"trigger": "SCHEDULED_BACKLOG",
"timestamp": 1539060316494,
"pivotVersion": 1,
"pivotType": "FlattenedTodoStats",
"todoCount": "todoCount",
"pivots": [
{
"state": "BACKLOG",
"type": "NA"
}
]
}
You can either use Jolt Transform (or) ReplaceText processors for this case.
As you are having some attributes to the flowfile so use ReplaceText processor
In ReplaceMent Value configure as
{
"projectId": "${projectId}",
"ticketId": "${ticketId}",
"trigger": "${trigger}",
"timestamp": "${timestamp}",
"pivotVersion":"${pivotVersion}",
"pivotType":"${pivotType}",
"todoCount":"${todoCount}",
"pivots[]": {
"*": {
"state": "${state}",
"type": "${type}"
}
}
}
Substitute all the attribute names(${projectId}..etc) with your attribute names.
Use Replacement Strategy as AlwaysReplace
(or)
If you want to use Jolt for this case then
Use default operation to replace your attribute values and prepare json message
Example:
Jolt Specification
[{ "operation": "shift", "spec": { "z":"z" } }, { "operation": "default", "spec": { "projectId": "${projectId}", "ticketId": "${ticketId}", "trigger": "${trigger}", "timestamp": "${timestamp}", "pivotVersion":"${pivotVersion}", "pivotType":"${pivotType}", "todoCount":"${todoCount}", "pivots[]": { "*": { "state": "${state}", "type": "${type}" } } } }]
As i don't have any attribute values, so my output json is having all empty values in it.
Change the spec jolt spec as per your requirements.

Transform Json object to an array of key value pairs in NiFi

I have a json object in this form
{
"email" : "test#gmail.com",
"name" : "somename",
"age" : "someage"
.
.
.
}
I want to to translate the above json to
[{
"key" : "email",
"value": "test#gmail.com"
},
{
"key" : "name",
"value": "somename"
},
{
"key" : "age",
"value": "someage"
}]
I want to do the above transformation using available NiFi processors. Also, in my requirement, json object fields are dynamic and I need to build a solution to transform the object as an array of objects with key and value fields. Any suggestion would be appreciated.
You can use the following Spec in JoltTransformJSON:
[
{
"operation": "shift",
"spec": {
"*": {
"$": "[#2].Key",
"#": "[#2].Value"
}
}
}
]
Screenshot

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"
}
}
]