Jolt Specification - etl

I have the following input format
INPUT.JSON
[
{
"name": "adam",
"age": 12,
"address": {
"city": "delhi",
"country": "india",
"zip": "110011"
}
},
{
"name": "louis",
"age": 23,
"address": {
"city": "goa",
"country": "india",
"zip": "110022"
}
}
]
After applying jolt transformation i want to get the following output
DESIRED OUTPUT.JSON
[
{
"name": "adam",
"age": 12,
"address": {
"current_city": "delhi", //change here
"current_country": "india", //change here
"zipode": "110011" //change here
}
},
{
"name": "louis",
"age": 23,
"address": {
"current_city": "goa", //change here
"current_country": "india", //change here
"zipode": "110022" //change here
}
}
]
Can you please help me with the jolt spec.
thanks

This might help,
[
{
"operation": "shift",
"spec": {
"*": {
"name": "[&1].name",
"age": "[&1].age",
"address": {
// Shifting inside address object
"city": "[&2].address.current_city",
"country": "[&2].address.current_country",
"zip": "[&2].address.zipode"
}
}
}
}
]

Related

Can I delete one item in JSON by jsonpath use golang

I have a json like this:
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
And get a json path expression: $.phoneNumbers.*.type
How can I delete the keys by this expression in Go?

Jolt transformation for spatial data (from geojson to arcGIS json)

I'm new to Jolt transformation and am trying to write a spec for spatial data. I'm unable to get the
expected output especially with the data for the coordinates. Any help would greatly be appreciated.
Below is the input I get:
{
"type": "Feature Collection",
"c rs": {
"type": "name",
"properties": {
"name": "4326"
}
},
"features": [
{
"type": "Feature",
"id": 1,
"geometry": {
"type": "Point",
"coordinates": [
-122.682207049,
45.526159497
]
},
"properties": {
"F ID": 1,
"Place Name": "Boyd Coffee",
"Place Address": "404 NW 11th Ave Portland Oregon",
"Score": 100,
"latitude": 45.526159497,
"longitude": -122.682207049
}
},
{
"type": "Feature",
"id": 2,
"geometry": {
"type": "Point",
"coordinates": [
-122.677518466,
45.525246078
]
},
"properties": {
"F ID": 2,
"Place Name": "John's Coffee Shop",
"Place Address": "301 NW Broadway St Portland Oregon",
"Score": 100,
"lat": 45.525246078,
"l on": -122.677518466
}
},
{
"type": "Feature",
"id": 3,
"geometry": {
"type": "Point",
"coordinates": [
-122.68287749,
45.526496967
]
},
"properties": {
"F ID": 3,
"Place Name": "Starbucks",
"Place Address": "1134 NW St Portland Oregon",
"Score": 100,
"latitude": 45.526496967,
"longitude": -122.68287749
}
}
]
}
The expected output is -
"features": [
{
"attributes": {
"F ID": 1,
"Place Name": "Boyd Coffee",
"Place Address": "404 NW 11th Ave Portland Oregon",
"Score": 100,
"latitude": 45.526159497,
"longitude": -122.682207049
},
"geometry": {
"x": -122.68220704900002,
"y": 45.526159496999998
}
},
{
"attributes": {
"FID": 2,
"Place Name": "John's Coffee Shop",
"Place Address": "301 NW Broadway St Portland Oregon",
"Score": 100,
"latitude": 45.525246078,
"longitude": -122.677518466
},
"geometry": {
"x": -122.677518466,
"y": 45.52524607799999
}
}
]
I've come up with spec as below, and not getting the expected out. Can anyone help please?
The spec is:
[
{
"operation": "shift",
"spec": {
"features": {
"*": {
"properties": "features[&1].attributes",
"geometry": {
"coordinates": {
"0": "features[&].geometry.x",
"1": "features[&].geometry.y"
}
}
}
}
}
}
]
Change the below in your spec
"0": "features[&].geometry.x", -> "0": "features[&3].geometry.x",
"1": "features[&].geometry.y", -> "1": "features[&3].geometry.y",
[
{
"operation": "shift",
"spec": {
"features": {
"*": {
"properties": "features[&1].attributes",
"geometry": {
"coordinates": {
"0": "features[&3].geometry.x",
"1": "features[&3].geometry.y"
}
}
}
}
}
}
]

Jolt Spec for Shift Operation

I have the following JSON input
{
"Status": "PENDING",
"TaskId": "0000000001",
"EventType": "change",
"Timestamp": "2019-01-09-15.41.57.473940T01",
"Comment": "{\"comment\":[{\"createDate\":\"2019-01-09T15:41:57:473000-05:00\",\"type\":\"system\",\"text\":\"Assigned to: RAJ\",\"userId\":\"RAJ\",\"userName\":\"RAJA MADHIE\"},{\"createDate\":\"2019-01-09T15:45:59:150000-05:00\",\"type\":\"manual\",\"text\":\"Comments entered for 0000000001\",\"userId\":\"RAJ\",\"userName\":\"RAJA MADHIE\"},{\"createDate\":\"2019-01-09T15:49:09:586000-05:00\",\"type\":\"manual\",\"text\":\"Comments entered for 0000000001 - processed.\",\"userId\":\"RAJ\",\"userName\":\"RAJA MADHIE\"}]}"
}
and expecting the output to be something like:
{
"inputs": [
{
"richInput": {
"subjectId": "0000000001",
"body": {
"messageSegments": [
{
"type": "system",
"text": "Assigned to: RAJ"
}
]
},
"feedElementType": "FeedItem"
}
},
{
"richInput": {
"subjectId": "0000000001",
"body": {
"messageSegments": [
{
"type": "manual",
"text": "Comments entered for 0000000001"
}
]
},
"feedElementType": "FeedItem"
}
},
{
"richInput": {
"subjectId": "0000000001",
"body": {
"messageSegments": [
{
"type": "manual",
"text": "Comments entered for 0000000001-processed."
}
]
},
"feedElementType": "FeedItem"
}
}
]
}
I have tried to transform this using couple of JOLT Specs but no luck... Any suggestions or recommendations are much appreciated. Thanks!
You need to make comments a proper JSON structure, it will appear something as below.
{
"Status": "PENDING",
"TaskId": "0000000001",
"EventType": "change",
"Timestamp": "2019-01-09-15.41.57.473940T01",
"Comment": {
"comment": [
{
"createDate": "2019-01-09T15:41:57:473000-05:00",
"type": "system",
"text": "Assigned to: RAJ",
"userId": "RAJ",
"userName": "RAJA MADHIE"
},
{
"createDate": "2019-01-09T15:45:59:150000-05:00",
"type": "manual",
"text": "Comments entered for 0000000001",
"userId": "RAJ",
"userName": "RAJA MADHIE"
},
{
"createDate": "2019-01-09T15:49:09:586000-05:00",
"type": "manual",
"text": "Comments entered for 0000000001 - processed.",
"userId": "RAJ",
"userName": "RAJA MADHIE"
}
]
}
}
Spec file to transform this:
[
{
"operation": "shift",
"spec": {
"Comment": {
"comment": {
"*": {
"#(3,TaskId)": "inputs[&1].richInput.subjectId",
"type": "inputs[&1].richInput.body.messageSegments[0].type",
"text": "inputs[&1].richInput.body.messageSegments[0].text",
"#FeedItem": "inputs[&1].richInput.feedElementType"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"inputs": {
"*": {
"richInput": {
"subjectId": "inputs[&2].richInput.subjectId",
"body": "inputs[&2].richInput.body",
"feedElementType": "inputs[&2].richInput.feedElementType"
}
}
}
}
}
]

JoltTransformJSON processor in NiFi(json without any parent tag)

I need to transform a JSON data using the JoltTransformJSON inside Nifi, here's my spec which I am using for the transformation:
[{
"operation": "shift",
"spec": {
"*": {
"Header": {
"readOn": "created_date_time",
"fileName": "readFile"
},
"Data": {
"id": "Id",
"first_name": "First_Name",
"last_name": "Last_Name",
}
}
}}]
My input data:
[{
"Header": {
"readOn": "2017/04/18 10:55:05",
"fileName": "sample1.csv",
"recordNum": 1
},
"Data": {
"last_name": "Martin",
"id": 21,
"first_name": "Clarence"
}
}, {
"Header": {
"readOn": "2017/04/18 10:55:05",
"fileName": "sample.csv",
"recordNum": 2
},
"Data": {
"last_name": "Graham",
"id": 22,
"first_name": "Walter"
}
}]
Output what I m getting:
{
"created_date_time": ["2017/04/18 10:55:05", "2017/04/18 10:55:05"],
"readFile": ["sample1.csv", "sample2.csv"],
"Id": [21, 22],
"First_Name": ["Clarence", "Walter"],
"Last_Name": ["Martin", "Graham"]
}
The required output:
[{
"recordNum": 1,
"Header": {
"created_date_time": "2017/04/18 10:55:05",
"readFile": "getusroi.csv"
},
"Data": {
"Last_Name": "Martin",
"Id": 21,
"First_Name": "Clarence"
}
}, {
"recordNum": 2,
"Header": {
"created_date_time": "2017/04/18 10:55:05",
"readFile": "getusroi.csv"
},
"Data": {
"Last_Name": "Graham",
"Id": 22,
"First_Name": "Walter"
}
}]
Question: Can someone guide me where all I need to change my jolt spec to achieve the desired transformed output.
The trick is to walk back up the tree until you get to the element in the array, then you can reference that index in the target. Try this spec:
[
{
"operation": "shift",
"spec": {
"*": {
"Header": {
"recordNum": "[&2].recordNum",
"readOn": "[&2].&1.created_date_time",
"fileName": "[&2].&1.readFile"
},
"Data": {
"id": "[&2].&1.Id",
"first_name": "[&2].&1.First_Name",
"last_name": "[&2].&1.Last_Name"
}
}
}
}
]

Count Repeated Values in Json String and store value and label in another String

I need to count the number of repeating items in the json array , and show the repeating items as single item and count should be shown how many tiems it has repeated..
please check the below code and Output so that you can understand:
[{
"Name": "Jacob",
"Gender": "Male",
"City": "Newyork",
"State": "Newyork"
}, {
"Name": "Mason",
"Gender": "Male",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Ethan",
"Gender": "Male",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Noah",
"Gender": "Male",
"City": "Newyork",
"State": "NewYork"
}, {
"Name": "Sophia",
"Gender": "Female",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Emma",
"Gender": "Female",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Isabella",
"Gender": "Female",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Olivia",
"Gender": "Female",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Elizabeth",
"Gender": "Female",
"City": "Newyork",
"State": "Newyork"
}, {
"Name": "zoey",
"Gender": "Female",
"City": "Newyork",
"State": "Newyork"
}]
The desired output from json is
Newyork : 4Times,
chicago:3 times,
Please find the fiddle :
http://jsfiddle.net/vamsikrishna981/dMrNv/
Try this http://jsfiddle.net/RHYFC/
See LONG VERSION and SHORT VERSION on fiddle
Javascript
var result = {};
for(var i = 0; i < data.length; i++){
for(var item in data[i]){
if(!result[item]){
result[item] = {};
}
if(!result[item][data[i][item]]){
result[item][data[i][item]] = 0;
}
result[item][data[i][item]]++;
}
}

Resources