JOLT Transformation Merge Array of Objects - jolt

I am trying to create a jolt transformation for the below input;
{
"group1": [
{
"schema": "schemaA"
},
{
"key1": "val1",
"key2": "val2"
}
],
"group2": [
{
"schema": "schemaA"
},
{
"key1": "val1",
"key2": "val2"
}
]}
With the desired output of;
{
"group1": {
"schema": "schemaA",
"key1": "val1",
"key2": "val2"
},
"group2": {
"schema": "schemaA",
"key1": "val1",
"key2": "val2"
}}
The key 'schema' will always be present but I won't know what the key1,key2,etc values are. So I can't explicitly map them. Any help would be much appreciated!

Spec,
[
{
"operation": "shift",
"spec": {
"group*": {
"*": {
"key*": "&2.&",
"schema": "&2.&"
}
}
}
}
]

Related

Add Array Index using JOLT

I want to add a kind of line number or identifier with jolt for each array element.
Given Array:
[
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN"
},
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN"
}
]
Expected Result:
[
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN",
"id": 0
},
{
"key1": "value1",
"key2": "value2",
"neyN": "valueN",
"id": 1
}
]
I tried now default, shift and more, but was not able to find a right solution.
Can someone help me?
Thanks in advance
Marcus
Spec 1 : Group the keys along with id field using index number.
Spec 2 : Remove the index number key from the result array.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "&1",
"$": "&1.id"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"#": "[]"
}
}
}
]

Extract certain fields in a Json array based on a filter using JOLT

Please help. I am trying to extract certain fields from the below input based on some condition ( list of name and match whose key value is "key2"). But not getting the proper result.
json Input:
{"schemas": [
{
"name": "myschema",
"tables": [
{
"name": "myname",
"alias": "temp_alias",
"keys": [
{
"name": "value1",
"key": "key1",
"match": "match_val"
},
{
"name": "value21",
"key": "key2",
"match": "match_val2"
},
{
"name": "value22",
"key": "key2",
"match": "match_val2"
},
{
"name": "value3",
"key": "key3"
},
{
"name": "value4",
"key": "key4"
}
]
}
]
}
]
}
Expected Output: list of name and match whose key value is "key2".
{"key2": [
{
"name": "value21",
"match": "match_val2"
},
{
"name": "value22",
"match": "match_val2"
}
]
}
I have used the below spec but it is giving all the keys in output.
[{
"operation": "shift",
"spec": {
"schemas": {
"*": {
"tables": {
"*": {
"keys": {
"*": {
"key": {
"key2": {
"#2": "&"
}
}
}
}
}
}
}
}
}
}
]
Check this spec,
[
{
"operation": "shift",
"spec": {
"schemas": {
"*": {
"tables": {
"*": {
"keys": {
"*": {
"key": {
"key2": {
"#2": "#2[]"
}
}
}
}
}
}
}
}
}
}
]

How to change a name of one field in large Json using Jolt

I have a big Json document:
{ "field1": "value1",
"field2": "value2",
"field3": "value3",
...
"field1000": "value1000"
}
I want to change the name of one field (field3) to third_field
How to do it without writing specification like that:
[
{
"operation": "shift",
"spec": {
"field1": "field1",
"field2": "field2",
"field3": "third_field",
...
"field1000": "field1000"
}
}
]
This should work and essentially does an if then else
[
{
"operation": "shift",
"spec": {
//if
"field3": {
//$ - current value
"$": "third_field"
},
//else
"*": {
//$ - current value
//& - current key
"$": "&"
}
}
}
]

Jolt JSON Conversion from String value to Long

I am using Jolt to convert one json to another json. Everything is working fine except I want to convert String value to Long. Below is my Specs and input. I have use modify-overwrite-beta but no luck.
Specs -
[
{
"operation": "modify-overwrite-beta",
"spec": {
"timestamp": "=toLong(#(1,time))"
}
},
{
"operation": "shift",
"spec": {
"key1": "outputText1",
"key2": "outputText2",
"key3": "outputText3",
"time": "timestamp"
}
}
]
Input Json
{
"key1": "test1",
"time": "1499967627",
"key2": "test2",
"key3": "test3",
}
So in above input json how I can convert time value to Long
Expected Json :
{
"outputText1": "test1",
"timestamp": 1499967627,
"outputText2": "test2",
"outputText3": "test3",
}
Spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"timestamp": "=toLong(#(1,time))"
}
},
{
"operation": "shift",
"spec": {
"key1": "outputText1",
"key2": "outputText2",
"key3": "outputText3",
// pass timestamp thru
"timestamp": "timestamp"
}
}
]
In the first operation (modify) it was making "timestamp" be a long. But then in the 2nd operation, you were copying the String value from "time" to timestamp, instead of passing timestamp thru.

Postgres jsonb, nested array querying to hide specific fields

I have a jsonb data of following format, with nested arrays
{
"outerArray": [
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
},
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
}
]
}
I want to write a retrieve query on this, to maintain same json structure but hide fields "price", "details" , "otherDetail"and "someField"
The retrieved result should look like this
{
"outerArray": [
{
"innerArray": [
{
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
]
},
{
"innerArray": [
{
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
]
}
]
}
Can this be done?
Please always specify a version of PostgreSQL you are using. An example below should work fine for versions v9.5+.
I would approach this by building a JSONB object you need with jsonb_build_object() and jsonb_build_array() functions:
Sample query:
WITH test(data) AS ( VALUES
('{
"outerArray": [
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
},
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
}
]}'::JSONB)
)
SELECT
jsonb_build_object(
'outerArray',
array_agg(
jsonb_build_object(
'innerArray',
json_build_array(
json_build_object(
'innerMostArray',
innerArray->'innerMostArray')
)
)
)
) as result
FROM test t,
jsonb_array_elements(t.data->'outerArray') as outerElement,
jsonb_array_elements(outerElement->'innerArray') as innerArray;
Result:
result
----------------------------------------------------------------------------------------------------------------------------------------------------------
{"outerArray": [{"innerArray": [{"innerMostArray": [{"A1": "A1"}, {"B1": "B1"}]}]}, {"innerArray": [{"innerMostArray": [{"A1": "A1"}, {"B1": "B1"}]}]}]}
(1 row)