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

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"
}
}
}
]

Related

How can I use two projections in MongoDB at the same time?

I have documents I need to retrieve data from in this format:
{
"id": ObjectId("alskdjflqkjwr23"),
"field1": {
"morefields": values
},
"field2": {
"morefields": values
},
"field3": {
"morefields": values
},
"field14": {
"morefields": values
},
"importantField": {
"subfield1": values,
"subfield2": values,
"importantArray": [
{
"subsubfield1": values,
"importantSubArray": [
{
"subsubsubfield1": values,
"subsubsubfield2": values
},
{
"subsubsubfield1": values,
"subsubsubfield2": values
}
],
"importantValue": values
},
{
"subsubfield1": values,
"subsubfield2": values,
"subsubfield3": values
}
]
}
}
I need the importantValue and elements in the importantSubArray fields, but I can't get my query to return what I need without all of the extra data in the other fields. I just started using Mongo a week ago.
I have tried this query, but only one of the projections works, depending on which order I put them in. So I can either return only the array within importantField, but all 400 or so elements, not just the last 2, or I can return the last 2 elements of importantField.importantArray, but also return all of the other fields, subfields, subsubfields etc.
db.getCollection("my_data").find({},
{
"importantField.importantArray" : 1.0,
"importantField.importantArray": { $slice: -2 }
}
);
How can I get both of these to work at the same time? Thanks.
EDIT:
The expected output should look like this. I need the elements in importantSubArray array and importantValue field
{
"importantSubArray": [
{
"stuff": morestuff
},
],
"importantValue": value
I was able to combine the comment from #prasad_ with a slice by using an aggregation and projecting the fields I want in the first stage and then doing another projection in stage 2 and slicing for the last 2 elements.

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 use value as key lookup

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!

How to extract data from postgresql jsonb object in multiple levels

I have a table that contains json data with an 'signatures' array occurring multiple levels:
{
"packet": "data"
"signatures": [
{ "packet": "data" },
{ "packet": "data" }
]
"userIds": [
{
"packet": "data",
"signatures": [
{ "packet": "data" },
]
}
]
}
I know how to get all the signatures that occur on one level, i.e. all the signatures directly on the "packet" or all the signatures on all "userIDs":
select string_agg(signature->'packet'->>'data', ',')
from keys,
jsonb_array_elements(doc->'userIDs') userID,
jsonb_array_elements(userID->'signatures') signature
But is there a way to extract all the signatures without knowing on which level they are?

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