Question/problem
How can I use Jolt to insert an array into a specific object in an array?
input
The objects have a common set of attributes, but support optional attributes.
Specific objects need to get these optional attributes depending on their specific position in the array, i.e. second ([1]
). These optional attributes are to be added as part of the jolt transformation.
{ "array" : [ { "key" : "a key", "value" : "a value" }, { "key" : "another key", "value" : "another value" } ] }
expected
{ "array" : [ { "key" : "a key", "value" : "a value" }, { "key" : "another key", "value" : "another value", "values": ["extra value1", "extra value2"] } ] }
What has been tried
I’ve tried default
, modify-default-beta
, and modify-overwrite-beta
and none seem to behave the way I thought they did.
default
Seems like default ignores the array reference "[1]"
so tried the beta operations which seem to support this.
default spec
[ { "operation": "default", "spec": { "array": { "[1]": { "values": ["extra value1", "extra value2"] } } } } ]
default actual
{ "array" : [ { "key" : "a key", "value" : "a value" }, { "key" : "another key", "value" : "another value" } ] }
modify-default-beta
Resolves the array reference "[1]"
, but only applies the first element in the array from the spec and not the whole array
modify-default-beta spec
[ { "operation": "modify-default-beta", "spec": { "array": { "[1]": { "values": ["extra value1", "extra value2"] } } } } ]
modify-default-beta actual
{ "array" : [ { "key" : "a key", "value" : "a value" }, { "key" : "another key", "value" : "another value", "values" : "extra value1" // array is dumped } ] }
modify-overwrite-beta
Tried modify-overwrite-beta
and I get the same behaviour as modify-default-beta
.
modify-overwrite-beta spec
[ { "operation": "modify-overwrite-beta", "spec": { "array": { "[1]": { "values": ["extra value1", "extra value2"] } } } } ]
modify-overwrite-beta actual
{ "array" : [ { "key" : "a key", "value" : "a value" }, { "key" : "another key", "value" : "another value", "values" : "extra value1" // array is dumped } ] }
Interesting side note
Without the array reference on the input this is a fairly easy problem, it seems the behaviour is not what I am expecting when there is an array reference and the *-beta
operations.
simple default
I’ve gotten rid of the array from the input
simple default input
{ "element1": { "key": "a key", "value": "a value" }, "element2": { "key": "a key", "value": "another value" } }
simple default spec
[ { "operation": "default", "spec": { "element2": { "values": ["extra value1", "extra value2"] } } } ]
simple default actual
{ "element1" : { "key" : "a key", "value" : "a value" }, "element2" : { "key" : "another key", "value" : "another value", "values" : [ "extra value1", "extra value2" ] } }
Advertisement
Answer
This appears to be my misunderstanding of the usage of the spec.
It appears that you don’t insert an array, but each element that you want to insert.
[ { "operation": "modify-default-beta", "spec": { "array": { "[1]": { "values": { "[0]": "extra value1", "[1]": "extra value2" } } } } } ]
The spec above is inserting both values into the values attribute and specifically into element "[0]"
and element "[1]"
.
This also works with the modify-overwrite-beta
operation
[ { "operation": "modify-overwrite-beta", "spec": { "array": { "[1]": { "values": { "[0]": "extra value1", "[1]": "extra value2" } } } } } ]
the result of both of these specs is as follows:
{ "array" : [ { "key" : "a key", "value" : "a value" }, { "key" : "another key", "value" : "another value", "values" : [ "extra value1", "extra value2" ] } ] }