Jolt use value as key lookup - jolt

Giving the input:
{
"influencers": [
{
"influencer_field_name": "COULD_BE_WHATEVER_STRING"
}
],
"COULD_BE_WHATEVER_STRING": [
"abcdefg"
]
}
The output should be:
{
"influencer_id": "abcdefg"
}
How do i use Jolt to pick up the value of the influencer_field_name and lookup a key with that value?
Is this even possible in Jolt?
Thanks!

Answer for future reference...
After some fiddling i found the solution. Not sure this is the best way to do it, but here it is.
[
{
"operation": "shift",
"spec": {
"#(influencers[0].influencer_field_name)": {
"*": {
"#(2,&[0])": "influencer_id"
}
}
}
}
]
So basically we traverse to the path influencers[0].influencer_field_name and then we save the value with * so we can use it in &.
Then we go up a 2 levels and fetch the value of the key and extract the first element of the array #(2,&[0]).
That's it... Bye!

Related

MongoDB script creates an empty object when it finds a null object instead of ignoring it, how can I fix this?

I have a large script, the following section is the relevant part, but if people prefer I can post the whole script on request.
The script iterates through a list, looking for a field called colour and renames it color.
{
serviceAgreementRefList: {
$map : {
input: "$$this.relatedJson.serviceAgreementRefList",
in: {
$mergeObjects: [
"$$this",
{
$cond: [
{
$ne: [
"$$this.situation",
undefined
]
},
"situation": {
$mergeObjects: [
"$$this.situation",
{
color: "$$this.situation.colour",
}
]
}
},
{},
]
}
]
}
}
}
}
It works as expected for the most part, however, if the object situation exists but is null then the script creates an empty situation object.
I would like any null situation objects to remain null.
How can I achieve that?
I thought I could add an $or function to the $cond, but of course that doesn't worse and in fact makes the problem worse.
Can I use an $in function, containing two $ne functions, one for undefined and one for null?
I would imagine I can, but I can't get the syntax right.
So the solution here is to use the $eq function instead of $ne and compare the type of the field to what we're expecting it to be.
If the type matches what we expect then the field name will be modified, if it doesn't, then nothing will be changed.
this is what the $cond function should look like:
$cond: [
{
$eq: [
{ $type: "$$this.situation" },
"object"
]
},
{ <code to execute if true> },
{ <code to execute if false> }
]
if situation is populated then $type will return "object", if it is null then $type will return "null" and if it does not exist $type will return "missing".

Wiremock: how to validate ALL objects in an array

Using wiremock-standalone (version 2.29.1), I want to verify a request with its body holding an array of objects, containing optional properties.
For instance, consider this request:
Request body (JSON format)
{
"foo": [
{ "bar": "1" },
{ "qux": "oh hai" },
{ "bar": "ohnoes" }
]
}
And let's say I want to match requests only if all the foo.bar attributes are either present, or contain only a single digit (it's only for the sake of example). The example above should not match (the third object has a bar attributes with non-digits characters).
I tried different approches, and the closest I got is this:
{
"matchesJsonPath": {
"expression": "$.foo[*].bar",
"or": [
{ "matches": "^\\d$" },
{ "absent": true }
]
}
}
There are 2 problems:
if there is no bar attribute at all, the request does not match
if at least 1 bar attribute passes the check, then the whole request passes, even though other bar values are invalid (the example above passes)
Does anyone know how to create such a rule in wiremock?
I found a possible solution, maybe not the easiest but it works.
Following your example:
request:
{
"foo": [
{ "bar": "1" },
{ "qux": "oh hai" },
{ "bar": "ohnoes" }
]
}
This bodyPatterns file works for validate each field is present and it has the value given:
"bodyPatterns" : [
{
"matchesJsonPath": "$.foo[?(#.bar == '1')]"
},
{
"matchesJsonPath": "$.foo[?(#.qux == 'oh hai')]"
},
{
"matchesJsonPath": "$.foo[?(#.bar == 'ohnoes')]"
}
]
For reference, the following post helped me a lot:
Wiremock matchesJsonPath checking array values ignoring the order

jolt, Transforming Flatten List of Key/Value to Array of name, value

I have a dynamic list of flatten key value pairs and need to convert it to an array of objects with name and value elements.
Input
{
"something_Y":"somevalue",
"something_X" :"someOtheValue",
"st":"sv",
.
.
.
}
and for the expected result should be
{
"array":[
{
"name":something_Y",
"value":"somevalue"
},
{
"name":something_X",
"value":"someOtherValue"
},
{
"name":"st",
"value":"sv"
},
.
.
.
]
This is "style" of problem has an example provided on jolt demo site http://jolt-demo.appspot.com/#mapToList
An adaptation of this for your data would be:
[
{
"operation": "shift",
"spec": {
"*": {
// #2 means go three levels up the tree (count from 0),
// and ask the "root" node, how many of it's
// children have been matched.
//
// This allows us to put the name and the value into
// the same object in the "array" array.
"$": "array[#2].name",
"#": "array[#2].value"
}
}
}
]

find only matching elements from a JSON array in Ne DB/react native-local-mongo/mongo db

i am trying to read only matching keys from a JSON with a nested array.
here is my JSON.
data:[
{
"locale":"en_US",
"pages":[
{
pageName:"testpage1",
"messages":{
"m1":"v1",
"m2":""
}
},
{
pageName:"testpage2",
"messages":{
"m1":"v3",
"m2":"v4"
}
}
]
},
{
"locale":"in_L1",
"pages":[
{
pageName:"testpage1",
"messages":{
"m1":"i1",
"m2":"i2"
}
},
{
pageName:"testpage2",
"messages":{
"m1":"i3",
"m2":"i4"
}
}
]
}
]
I am trying the below query:
db['collec1'].find({locale:"en_US", pages:{$elemMatch:{pageName:"testpage1"},
{locale:1,"pages.pageName":1}}})
also tried,
db['collec1'].find({locale:"en_US", "pages.pageName":"testpage1"},{locale:1,"pages.pageName":1}}})
both are returning, the all the elements in the array as below:
[
{
"locale":"en_US",
pages:{pageName:["testpage1", "testpage2"]}]
}
]
Expected output is:
[
{
"locale":"en_US",
pages:{pageName:["testpage1"]}]
}
]
can someone help me where i am doing it wrong and what needs to be changed?
i am using this library which is a clone of mongodb called Ne DB.
db['collec1'].find({locale:"en_US", pages:{$elemMatch:{pageName:"testpage1"}}).
try with an empty project in the find method {} and then try like
the above example

Jolt Numeric Constant

In Jolt it is possible to assign a constant value to a field by prefixing the constant value with # as below.
{ "#2": "myField"}
But this constant is always added as a string value. How can I add it as a numeric value?
Use "default" or the newer "modify-default-beta".
[
{
"operation": "default",
"spec": {
"myField": 2
}
}
]