Map value to a key using JOLT Spec - json

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

Related

unable to convert json list to objects using jolt

I need to use jolt transform to do the below JSON transformation.
need to create new columns from the list from reeval column where sometimes we only one value and some times we get multiple values my input data :-
example 1:
{
"id":"1",
"reeval":["one","two"]
}
example 2:
{
"id":"2",
"reeval":["one","two","three"]
}
example 3:
{
"id":"3",
"reeval":["one"]
}
I have written jolt expresson as below
[
{
"operation": "shift",
"spec": {
"id": "id",
"reeval": {
"*": "&"
}
}
}
]
with above jolt expression is working fine but unable to add column name
output for above jolt is as below
example 1:
{
"id" : "1",
"0" : "one",
"1" : "two"
}
example 2:
{
"id" : "2",
"0" : "one",
"1" : "two",
"2" : "three"
}
here i am unable to change the names of the columns as i need to change colunms as below
my expected output after jolt transformation should be like
example 1:
{
"id":"1",
"reeval":"one",
"reeval1":"two"
}
example 2:
{
"id":"2",
"reeval":"one",
"reeval1":"two",
"reeval2":"three"
}
example 3:
{
"id":"3",
"reeval":"one"
}
Prepending &1 to the current ampersand would suffice in order to go one level up the tree, and to grab the key name in the first shift transformation, and then apply another to rename only the key with index zero such as
[
{
"operation": "shift",
"spec": {
"id": "id",
"reeval": {
"*": "&1&"
}
}
},
{
"operation": "shift",
"spec": {
"reeval0": "reeval",
"*": "&"
}
}
]

Jolt get value of key value pair where key equals host in array of key value pairs

I have a array of Key Value Pairs in my json object, and need to pull out a set value based on the key being equal to host.
{
"pairs" : [ {
"key" : "Host",
"value" : "site-a"
}, {
"key" : "User",
"value" : "user42"
}
}
I can't match based on position as it could be anywhere in the array of pairs, and the array can vary in size.
My Current Jolt spec looks like, but it's just listing each pair:
[
{
"operation": "shift",
"spec": {
"requestHeaderFields": {
"*": {
"value": "#(1,key)"
}
}
}
}
]
The current output is:
{
"Host" : "site-a",
"User-Agent" : "user42"
}
My desired output would be the following, noting the field name change:
{
"HostSite" : "site-a",
}
I am wondering if I first need to do a modify-overwrite-beta operation and then a shift?
This jolt will do the trick. The idea is to check when key has a Host value and then retrieve value:
[
{
"operation": "shift",
"spec": {
"pairs": {
"*": {
"key": {
"Host": {
"#(2,value)": "HostSite"
}
}
}
}
}
}
]

How can I combine two arrays to create a key value pair with Jolt?

I've already created a spec to convert my JSON input
{
"rows": [
{
"row": [
"row1",
"row2",
"row3"
],
"header": [
"header1",
"header2",
"header3"
]
},
{
"row": [
"row4",
"row5",
"row6"
],
"header": [
"header4",
"header5",
"header6"
]
}
]
}
to convert to key-value pairs as following object result :
{
"header1" : "row1",
"header2" : "row2",
"header3" : "row3",
"header4" : "row4",
"header5" : "row5",
"header6" : "row6"
}
Is this possible to do using Jolt?
Is there a copy/paste error in your input? Judging by your desired output, the second object's header array should be ["header4", "header5", "header6"]. If that's the case, this spec should work:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"header": {
"*": {
"*": {
"#(3,row[#2])": "&"
}
}
}
}
}
}
}
]
One option is to use the following shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*s": { // rows array
"*": {
"&(1,1)": { // row array
"*": {
"#": "#(3,header[&1])"
}
}
}
}
}
}
]
where
"*s": { stands for rows
"&(1,1)": { -> not immediate(zeroth) level but one more level up by using &(1, and grab the value there the first asterisk exists by &(..,1)
"#": "#(3,header[&1])" -> 3 levels needed as stated at the right hand side traverse the colon
as well in order to reach the level of &(1,1) which is used to
represent the "row" array along with &1 representation to go one level up the tree to reach the indexes of the array "row" while matching with the values of "row" through use of # on the left hand side
the demo on the site http://jolt-demo.appspot.com/ is :

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

Jolt transform so that data of an element is the key and the value is another elements data

I need to perform a Jolt transformation on the below example json:
[ {
"name" : "foo",
"dataSample" : "red"
}, {
"name" : "bar",
"dataSample" : "amber"
}]
I need the output to look like:
{
"foo": "red",
"bar": "amber"
}
so far i've managed to extract the name value as the key, but i'm lost as to how to get the dataSample value as the value for the transformed element. Here's the Jolt script I have so far:
[
{
"operation" : "shift",
"spec" : {
"*" : {
"name" : {
"*" : "&"
}
}
}
}
]
You need to go back up the tree to get the value of the "name" field, rather than using the current value (&). This should work:
[
{
"operation": "shift",
"spec": {
"*": {
"name": {
"#(1,dataSample)": "#(2,name)"
}
}
}
}
]
[
{
"operation": "shift",
"spec": {
"*": {
"dataSample": "#(1,name)"
}
}
}
]