I am using NiFi Jolt Processor to transform some JSON data.
My JSON field genre contains shared attributes and an array that contains the list of actual genre names.
I needed to transform the "genre" attribute to become an array of "genres" containing the list of the common attributes and the different genre names.
I have the following input JSON:
{
"programIdentifier": "9184663",
"programInstance": {
"genre": {
"source": "GN",
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"genres": [
"Miniseries",
"Drama"
]
}
}
}
I have tried the following spec:
[{
"operation": "shift",
"spec": {
"programIdentifier": ".&",,
"genre": {
"source": "genres[].source.value",
"locked": "genres[].locked",
"lastModifiedDate": "genres[].lastModifiedDate",
"lastModifiedBy": "genres[].lastModifiedBy",
"genres": {
"*": "genres[&0].name"
}
}
}]
This my expected output:
{
"programIdentifier": "9184663",
"programInstance": {
"genres": [
{
"source": {
value: "GN"
}
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"name": "Miniseries"
},
{
"source": {
value: "GN"
}
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"name": "Drama"
}
]
}
}
But it's coming out as:
{
"programIdentifier": "9184663",
"programInstance": {
"genres": [
{
"source": {
"value": "GN"
},
"name": "Miniseries"
}, {
"locked": false,
"name": "Drama"
}, {
"lastModifiedDate": 1527505462094
}, {
"lastModifiedBy": "Some Service"
}],
}
}
Is it what you want to achieve?
[
{
"operation": "shift",
"spec": {
"programIdentifier": ".&",
"programInstance": {
"genre": {
"genres": {
"*": {
"#2": {
"source": "programInstance.genres[&2].source[]",
"locked": "programInstance.genres[&2].locked",
"lastModifiedDate": "programInstance.genres[&2].lastModifiedDate",
"lastModifiedBy": "programInstance.genres[&2].lastModifiedBy"
},
"#": "programInstance.genres[&1].name"
}
}
}
}
}
}
]
Related
Sample Result : need to sort this (name will always be constant, only value will change)
{
"sampleJson": { "loop1": {
"userId": "1",
"loop2": {
"loop3": [
{
"name": "hindi",
"value": "abc"
},
{
"name": "telugu",
"value": "xyz"
}
]
}
},
"loop1": {
"userId": "2",
"loop2": {
"loop3": [
{
"name": "hindi",
"value": "def"
},
{
"name": "telugu",
"value": "ghi"
}
]
}
},
"loop1": {
"userId": "1",
"loop2": {
"loop3": [
{
"name": "hindi",
"value": "jkl"
},
{
"name": "telugu",
"value": "mno"
}
]
}
}
}
}
After performing elastic Sorting, result should look like below:
{
"sampleJson": {
"loop1": {
"userId": "1",
"loop2": {
"loop3": [
{
"name": "hindi",
"value": "jkl"
},
{
"name": "telugu",
"value": "mno"
}
]
}
},
"loop1": {
"userId": "1",
"loop2": {
"loop3": [
{
"name": "hindi",
"value": "abc"
},
{
"name": "telugu",
"value": "xyz"
}
]
}
}
}
}
below Elastic sorting is not working: not sure what is wrong with this. any help that would be great.
{
"from": 0,
"size": 25,
"query": {
"bool": {
"must": [
{
"match": {
"userId": "1"
}
}
]
}
},
"sort": [
{
"sampleJson.loop1.loop2.loop3.value": {
"order":"asc",
"nested": {
"path": "sampleJson",
"filter": {
"terms" : {
"sampleJson.loop1.loop2.loop3.name": [
"telugu"
] }
}
}
}
}
]
}
I have following code:
%dw 2.0 output application/json var attributeIdMapping =
${vault::attributeIdMapping}
--- {
"objectTypeId": 3,
"attributes": vars.requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name == key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
attributeIdMapping:
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
vars.requestBody:
{
"displayName": "TestMulesoft2",
"description": "Test"
}
But my filter shows null in the end. As I understand key is not passed to level below map itself. How can I get this working?
I put all the input data inside the script to simplify reproduction. The problem seems to be using the operator == to compare a string and a key types returns empty. Using the operator ~= which attempts to coerce appears to return the expected result:
%dw 2.0
output application/json
var attributeIdMapping =
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
var requestBody = {
"displayName": "TestMulesoft2",
"description": "Test"
}
--- {
"objectTypeId": 3,
"attributes": requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name ~= key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
Output:
{
"objectTypeId": 3,
"attributes": {
"objectTypeAttributeId": "18",
"objectAttributeValues": [
{
"value": "TestMulesoft2",
"key": "displayName"
}
],
"objectTypeAttributeId": "86",
"objectAttributeValues": [
{
"value": "Test",
"key": "description"
}
]
}
}
How I can access to a depth key value from a nested hash?
I want to extract values inside of _Data, I want to extract the field and size inside data[], to be printed like fieldvalue_sizevalue
elemento3_3
I have this code but only get the all the data keys and values and i had some troubles making this iteration, I am new at ruby
_Data[0][:checks].each do |intera|
puts "#{itera[:data]}
end
this is the nested array
_Data =[
{
"agent": "",
"bottom_comments": [],
"checks": [
{
"title": "new",
"data": [{
"field": "elemento1",
"value": "0",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento2",
"value": "0",
"size":"4",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento3",
"value": "0",
"size":"3",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento4",
"value": "0",
"size":"17",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento5",
"value": "0",
"size":"12",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento6",
"value": "0",
"size":"19",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento7",
"value": "0",
"size":"9",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento8",
"value": "0",
"size":"10",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento11",
"value": "0",
"size":"14",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento19",
"value": "0",
"size":"14",
}
]
},
{
"type": "condiciones",
"elementos": [
{
"table": [
{
"title": "",
"name": "radio",
},
{
"title": "",
"name": "xenon",
},
{
"title": "",
"name": "aluminio",
},
{
"title": "",
"name": "boro",
},
{
"title": "",
"name": "oro",
},
{
"title": "",
"name": "bromo",
},
{
"title": "",
"name": "oxigeno",
}
]
}
]
}
]
}
]
_Data[0][:checks].each do |intera|
intera[:data].each do |data|
puts "#{data[:filed]}_#{data[:value]}"
end
end
Hope this may help you.
Here is one line code to print the values if they exist. Let me know if any further details on any of the methods used will be helpful.
_Data[0][:checks].pluck(:data).flatten.compact.each{|i| puts "#{i[:field]}_#{i[:size]}" if i[:field] && i[:size]}
My input JSON:
[
{
"label": [
{
"name": "abc"
}
]
},
{
"label": [
{
"name": "xyz"
}
]
}
]
My spec:
[{
"operation": "shift",
"spec": {
"*": {
"label": {
"0": {
"name": "name"
}
}
}
}
}]
Expected output:
[{
"name": "abc"
}, {
"name": "xyz"
}]
Generated output:
{
"name" : [ "abc", "xyz" ]
}
How do I not combine the array in the spec?
Try this spec,
[
{
"operation": "shift",
"spec": {
"*": {
"label": {
"*": {
"name": "[&3].name"
}
}
}
}
}
]
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"
}
}
}
}
}
]