Updating sub document map in n1q1 query CB - couchbase

Here is my document in CB,
{
"users": {
"11111": "ACTIVE",
"22222": "ACTIVE",
"33333": "ACTIVE",
"44444": "ACTIVE",
"55555": "ACTIVE",
"666666": "ACTIVE",
"777777": "ACTIVE",
"888888": "ACTIVE"
}
}
I want to update all keys of this users map to 0 instead of active.
output expected
{
"users": {
"11111": 0,
"22222": 0,
"33333": 0,
"44444": 0,
"55555": 0,
"666666": 0,
"777777": 0,
"888888": 0
}
}
I tried this
UPDATE bucketname SET x=0 FOR x in bucketname.users END WHERE
meta().id='%documentKey%'

UPDATE mybucket AS b
SET b.users = OBJECT n:0 FOR n:v IN b.users END
WHERE ......
Construct new object and and set the value
FOR n:v IN b.users END
iterates each field of b.users (n holds name, v holds value)
OBJECT n:0
Construct new object field name from n and value is 0 constant

Related

Mule condense data based on a category

Example below. I've got a set of account numbers, with an account attribute. For each account_number there are three categories, and I would like the sum for each account number based on each balance in DataWeave.
Data input
[
{
Account_Number: 1,
Account: 5,
Category: "A",
Balance: 500
},
{
Account_Number: 1,
Account: 5,
Category: "A",
Balance: 700
},
{
Account_Number: 1,
Account: 5,
Category: "B",
Balance: 300
},
{
Account_Number: 1,
Account: 5,
Category: "C",
Balance: 100
},
{
Account_Number: 2,
Account: 10,
Category: "B",
Balance: 300
},
{
Account_Number: 2,
Account: 10,
Category: "B",
Balance: 800
}
]
Data Output
[
{
Account_Number: 1,
Account: 5,
CategoryA_Balance: 1200,
CategoryB_Balance: 300,
CategoryC_Balance: 100
}
{
Account_Number: 2,
Account: 10,
CategoryA_Balance: 0,
CategoryB_Balance: 1100,
CategoryC_Balance: 0
}
]```
I assume Categories are dynamic. If not, you can replace the Categories variable with a static array.
%dw 2.0
output application/json
var byAcctNbr = payload groupBy ($.Account_Number)
var categories = payload..Category distinctBy $
---
keysOf(byAcctNbr) map ((acctNbr) ->
do {
var item = byAcctNbr[acctNbr]
var outItem = (item[0] default {}) - "Balance" - "Category"
var balances = categories reduce ((category, acc={}) ->
do {
var accounts = item filter ($.Category == category)
---
acc ++ (
("Category" ++ category ++ "_Balance"): if (isEmpty(accounts)) 0
else sum (accounts.Balance)
)
})
---
outItem ++ balances
}
)
A Similar solution to sudhish. Breaking down the solution for better understanding
distinctBy Since .. will give you all the categories present in the input. DistinctBy will remove duplicates and you will have [A,B,C]
groupBy to group based over details of each account number
(item[0] - "Balance" - "Category") Since we require AccountNumber and Account only once so used item[0] and "-" to eliminate Balance and Category since we need to perform some conditional based logic further
pluck to convert the object with account number as key to array
map iterates over the details of each account number
map over the categories will yield you [A,B,C] for both the account numbers
filter to check if the Category present in the top level map matches the categories present in the variable. if (true) then sum(Balance) else 0
sum to add based on the categories matched using filter
%dw 2.0
output application/json
var categories = payload..Category distinctBy $
---
payload groupBy $.Account_Number pluck $ map(item,index)->{
(item[0] - "Balance" - "Category"),
(categories map (cat)->{
("Category" ++ cat ++ "_Balance"):
if (isEmpty(item filter ($.Category == cat)))
0
else
sum((item filter ($.Category == cat)).Balance)
})
}

How to extract value from JSON Path extractor based on condition

I would like to get the value of the "currentApproversStr:" based on the condition "status":"Ready for Review" from the below JSON Response body of a HTTP sampler and pass to following HTTP sampler.
I tried the below but it's not working
Names of created variables: currentApproversStr
JSON Path expressions: $.[?((#.currentApproversStr == "Validation, Civa" || #.currentApproversStr == "Validation, Darla" || #.currentApproversStr == "Validation, Bittl" || #.currentApproversStr == "Validation, Cha" || #.currentApproversStr == "Validation, Barnett" ) && #.status== "Ready for Review")]
Match No: -1 OR 1
But Dummy Sampler returns the Results
We can't guarantee the order of the "timecardId" block with the "status":"Ready for Review" i.e some times 2 nd place, some times last. In this it's 2nd block. So not sure Match No: what should i give
[
{
"timecardId": 170803,
"entryHeaderId": "db9341a9-32e8-4d45-a858-a88b75a42cef",
"startsOn": "2021-10-24T00:00:00",
"endsOn": "2021-10-30T00:00:00",
"worksightStatus": "SignedOff",
"projectId": 1977,
"userId": 60874,
"status": "Submitted for Approval",
"batchId": 39814,
"emergencyType": "",
"htgDealMemoId": "0d0ff42b-5c4b-4695-b527-34dfc64585e5",
"unionId": "1c77c660-28fc-4e40-b557-132f3da39597",
"currentApproversStr": "Perf, PA",
"commentStr": "",
"commentUserName": "",
"commentCreatedAt": "1900-01-01T00:00:00",
"occupationCode": "TECHNICIAN",
"activeApprovalFlowId": 166669,
"isAllowanceOnly": false,
"departmentId": null,
"datePosted": null
},
{
"timecardId": 170807,
"entryHeaderId": "c9809446-b01f-4f42-add6-9b441c3d0114",
"startsOn": "2021-10-17T00:00:00",
"endsOn": "2021-10-23T00:00:00",
"worksightStatus": "Outstanding",
"projectId": 1977,
"userId": 60874,
"status": "Ready for Review",
"batchId": 39815,
"emergencyType": "",
"htgDealMemoId": "0d0ff42b-5c4b-4695-b527-34dfc64585e5",
"unionId": "1c77c660-28fc-4e40-b557-132f3da39597",
"currentApproversStr": "Validation, Civa",
"commentStr": "",
"commentUserName": "",
"commentCreatedAt": "1900-01-01T00:00:00",
"occupationCode": "TECHNICIAN",
"activeApprovalFlowId": 166674,
"isAllowanceOnly": false,
"departmentId": null,
"datePosted": null
},
{
"timecardId": 170802,
"entryHeaderId": "db9341a9-32e8-4d45-a858-a88b75a42cef",
"startsOn": "2021-10-24T00:00:00",
"endsOn": "2021-10-30T00:00:00",
"worksightStatus": "SignedOff",
"projectId": 1977,
"userId": 60874,
"status": "Submitted for Approval",
"batchId": 39814,
"emergencyType": "",
"htgDealMemoId": "0d0ff42b-5c4b-4695-b527-34dfc64585e5",
"unionId": "1c77c660-28fc-4e40-b557-132f3da39597",
"currentApproversStr": "Perf, PA",
"commentStr": "",
"commentUserName": "",
"commentCreatedAt": "1900-01-01T00:00:00",
"occupationCode": "TECHNICIAN",
"activeApprovalFlowId": 166669,
"isAllowanceOnly": false,
"departmentId": null,
"datePosted": null
}
]
PROBLEM:
The reason is that you misunderstand the way JSON extractor works. This feature allows you extract many variables in one setting, but number of Names of created variables = number of JSON Path expressions = number of Default Values.
For example, you want to extract 2 variables:
Names of created variables: var_name_1; var_name_2
JSON Path expressions: json_expression_1; json_expression_2
Default Values: default_1; default_2
(Note: remember using semicolon (;) to separate values)
But you set 1 Variable, 1 json expression with MANY default values --> mismatch.
SOLUTION:
You can setup like this:
Names of created variables: currentApproversStr
JSON Path expressions: $.[?(#.status== "Ready for Review")].currentApproversStr
Match No: -1
Default Values: NOT_FOUND
Result:
currentApproversStr_1=Validation, Civa
currentApproversStr_matchNr=1
"Match No" works as follows: if your query returns more than 1 result:
0 - returns random result
-1 - returns ALL results in form of:
currentApproversStr_1 - first match
currentApproversStr_2 - second match
etc.
currentApproversStr_matchNr - total number of matches
any positive integer - returns the given match
It applies not only to JSON Extractor but to all other JMeter PostProcessors which extract values from responses.
You can see generated JMeter Variables using Debug Sampler and View Results Tree listener combination:

How do I implement an array from a database?

I want to implement an array from the received data that will contain objects with the same identifiers.
pool.query('SELECT * FROM columnslist INNER JOIN tableslist ON columnKey = keyTable', (error, result) => {
response.send(result);
});
After executing this code, I get the expected result:
[
{
"id": 1,
"columnOne": "qeqeqq qq wq qw wwqqwdqd",
"columnTwo": "qdqdqdq wdqdqwqwd",
"columnThree": "dqwdq qw qqsvds",
"columnFour": "svsdvsxcvscsv svd ds",
"columnFive": "sdvsdvsdvs ds sdd",
"columnKey": 1,
"keyTable": 1,
"name": "Test"
},
{
"id": 3,
"columnOne": "qdqwdwq",
"columnTwo": "dqdqd",
"columnThree": "qdqdwq",
"columnFour": "wdqwdqwq",
"columnFive": "wdqdqw",
"columnKey": 2,
"keyTable": 2,
"name": "Qqeqeqeq"
},
{
"id": 4,
"columnOne": "qdqwdwq",
"columnTwo": "dqdqd",
"columnThree": "qdqdwq",
"columnFour": "wdqwdqwq",
"columnFive": "wdqdqw",
"columnKey": 2,
"keyTable": 2,
"name": "Qqeqeqeq"
}
]
Tell me how you can implement or process the response to get this result? I need an array to be created in the array, as I wrote earlier, with the same identifiers:
[
[{"keyTable": 1,"name": "Test"...}],
[{"columnKey": 2,"keyTable": 2...}, [{"columnKey": 2,"keyTable": 2...}]
]
Thanks to!
You can typecast objects into an associative array like this
$array = (array) $yourObject;
In your case
$array = (array) $result;
This will help out you
You can use JSON_ARRAYAGG() AND GROUP BY, for your example the query is :
SELECT ALL
columnKey, keyTable,
JSON_ARRAYAGG(JSON_OBJECT(
'id', id,
'columnOne', columnOne,
'columnTwo', columnTwo,
'columnThree', columnThree,
'columnFour', columnFour,
'columnFive', columnFive,
'columnKey', columnKey,
'keyTable', keyTable,
'name', name
)) AS jsonData
FROM columnslist
INNER JOIN tableslist ON columnKey = keyTable
GROUP BY columnKey, keyTable
https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-arrayagg

python : Parsing json file into list of dictionaries

I have the following json file annotations
and here is a screenshot form it.tree structure of the json file
I want to parse it and extract the following info
here is a link which I take this screenshot form it Standard Dataset Dicts
I tried to use this code which is not working as expected.
def get_buildings_dicts(img_dir):
json_file = os.path.join(img_dir, "annotations.json")
with open(json_file) as f:
imgs_anns = json.load(f)
dataset_dicts = []
for idx, v in enumerate(imgs_anns):
record = {}
filename = os.path.join(img_dir, v["imagePath"])
height, width = cv2.imread(filename).shape[:2]
record["file_name"] = filename
record["image_id"] = idx
record["height"] = height
record["width"] = width
annos = v["shapes"][idx]
objs = []
for anno in annos:
# assert not anno["region_attributes"]
anno = anno["shape_type"]
px = anno["points"][0]
py = anno["points"][1]
poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
poly = [p for x in poly for p in x]
obj = {
"bbox": [np.min(px), np.min(py), np.max(px), np.max(py)],
"bbox_mode": BoxMode.XYXY_ABS,
"segmentation": [poly],
"category_id": 0,
}
objs.append(obj)
record["annotations"] = objs
dataset_dicts.append(record)
return dataset_dicts
here is an expected output of the final dict items:
{
"file_name": "balloon/train/34020010494_e5cb88e1c4_k.jpg",
"image_id": 0,
"height": 1536,
"width": 2048,
"annotations": [
{
"bbox": [994, 619, 1445, 1166],
"bbox_mode": <BoxMode.XYXY_ABS: 0>,
"segmentation": [[1020.5, 963.5, 1000.5, 899.5, 994.5, 841.5, 1003.5, 787.5, 1023.5, 738.5, 1050.5, 700.5, 1089.5, 663.5, 1134.5, 638.5, 1190.5, 621.5, 1265.5, 619.5, 1321.5, 643.5, 1361.5, 672.5, 1403.5, 720.5, 1428.5, 765.5, 1442.5, 800.5, 1445.5, 860.5, 1441.5, 896.5, 1427.5, 942.5, 1400.5, 990.5, 1361.5, 1035.5, 1316.5, 1079.5, 1269.5, 1112.5, 1228.5, 1129.5, 1198.5, 1134.5, 1207.5, 1144.5, 1210.5, 1153.5, 1190.5, 1166.5, 1177.5, 1166.5, 1172.5, 1150.5, 1174.5, 1136.5, 1170.5, 1129.5, 1153.5, 1122.5, 1127.5, 1112.5, 1104.5, 1084.5, 1061.5, 1037.5, 1032.5, 989.5, 1020.5, 963.5]],
"category_id": 0
}
]
}
I think the only tricky part is dealing with the nested lists but a handful of coprehensions can probably make life easier for us.
Try:
import json
new_images = []
with open("merged_file.json", "r") as file_in:
for index, image in enumerate( json.load(file_in)):
#height, width = cv2.imread(filename).shape[:2]
height, width = 100, 100
new_images.append({
"image_id": index,
"filename": image["imagePath"],
"height": height,
"width": width,
"annotations": [
{
"category_id": 0,
#"bbox_mode": BoxMode.XYXY_ABS,
"bbox_mode": 0,
"bbox": [
min(x for x,y in shape["points"]),
min(y for x,y in shape["points"]),
max(x for x,y in shape["points"]),
max(y for x,y in shape["points"])
],
"segmentation": [coord for point in shape["points"] for coord in point]
}
for shape in image["shapes"]
],
})
print(json.dumps(new_images, indent=2))

"Cannot deserialize the current JSON object" How to fix this error?

I'm not sure how to fix the following error and am looking for some help in this code I found that I have attempted to adapt for use in application embedded VB.Net:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Object]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'error', line 1, position 9.
Below is the code That is generating this error
Dim OutputText As String
Dim Worksteps = JsonConvert.DeserializeObject(Of List(Of Object))(responseFromServer)
Dim token As JToken
Dim SheetName As String
Dim SheetStatus As String
Dim Completed As Boolean
For Each value As Object In Worksteps
token = JObject.Parse(value.ToString())
SheetName = token.SelectToken("name")
SheetStatus = token.SelectToken("status")
Dim parts() As String = SheetName.Split(" "c)
If parts(0) = "Sheet_1" Then
If SheetStatus = "COMPLETED" Then
Completed = True
Exit For
End If
End If
Next value
Select Case Completed
Case True
OutputText = "Sheet_1 Status is COMPLETED"
Case False
OutputText = "Sheet_1 Status is NOT COMPLETED"
End Select
Here is the JSON I'm attempting to parse. My assumption is that I somehow need to first parse for the Worksteps before trying to make a list of worksteps but I'm not sure how to do that.
{
"error": null,
"worksteps": [
{
"id": "_210504_125916572_029875",
"name": "Sheet_1 4/4",
"job": {
"id": "060671-21",
"name": "2021 Laramie High School"
},
"status": "COMPLETED",
"amountPlanned": 544,
"wastePlanned": 60,
"amountProduced": 169,
"wasteProduced": 69,
"deviceId": "114",
"types": [
"ConventionalPrinting"
],
"sequenceType": "SheetfedPrinting",
"start": "2021-05-05T09:46:06-05:00",
"end": "2021-05-05T09:48:38-05:00",
"startPlanned": "2021-05-05T07:52:22-05:00",
"endPlanned": null,
"setuptimePlanned": 0,
"prodtimePlanned": 0,
"actualTimes": [
{
"timeTypeGroupName": "Production time",
"timeTypeName": "Execution time",
"duration": 33
},
{
"timeTypeGroupName": "Production time",
"timeTypeName": "Setup time",
"duration": 79
},
{
"timeTypeGroupName": "Auxiliary time",
"timeTypeName": "Auxiliary time",
"duration": 40
},
{
"timeTypeGroupName": "",
"timeTypeName": "",
"duration": 0
}
]
},
{
"id": "_210506_072306983_020035",
"name": "Sheet_2 4/4",
"job": {
"id": "060671-21",
"name": "2021 Laramie High School"
},
"status": "WAITING",
"amountPlanned": 0,
"wastePlanned": 0,
"amountProduced": 0,
"wasteProduced": 0,
"deviceId": "XL106_Pool",
"types": [
"ConventionalPrinting"
],
"sequenceType": "SheetfedPrinting",
"start": null,
"end": null,
"startPlanned": null,
"endPlanned": null,
"setuptimePlanned": 0,
"prodtimePlanned": 0,
"actualTimes": null
}
]
}
Here is the solution I came up with to convert JSON sample above to XML and then extract the values for the sheet and status:
'Parse the JSON responseFromServer into an XMLDocument
Dim xdoc As New XmlDocument
' "job_worksteps" is the name being provided
xdoc = JsonConvert.DeserializeXmlNode(responseFromServer,"job_worksteps")
' Look in the XML to determine the if a specified sheet has been completed
Dim nodes As XmlNodeList = xdoc.DocumentElement.SelectNodes("/job_worksteps/worksteps")
Dim Completed As Boolean
Dim SheetName, SheetStatus, OutputText As String
Dim TargetSheet As String = "Sheet_" + triggerEvent.String2 ' Integer as string included in the Prinergy RBA Trigger HTTP Post
' Loop through the XML nodes to find the SheetName node "name" and SheetStatus node "status"
For Each node As XmlNode In nodes
SheetName = node.SelectSingleNode("name").InnerText
SheetStatus = node.SelectSingleNode("status").InnerText
Dim parts() As String = SheetName.Split(" ")
' Validate the TargetSheet name has a Status of "COMPLETED"
If parts(0) = TargetSheet Then
If SheetStatus = "COMPLETED" Then
Completed = True
Exit For
End If
End If
Next
' Format the output we want for this test
' (The final Boolean value will actually be used to trigger another function if it is True)
Select Case Completed
Case True
OutputText = "Sheet_1 Status is COMPLETED"
Case False
OutputText = "Sheet_1 Status is NOT COMPLETED"
End Select
Here is the resulting XML from some additional code to append it to the OutputText variable:
Sheet_1 Status is COMPLETED
<job_worksteps>
<error />
<worksteps>
<id>_210505_073301677_027121</id>
<name>Sheet_1 4/4</name>
<job>
<id>060671-21</id>
<name>2021 Laramie High School</name>
</job>
<status>COMPLETED</status>
<amountPlanned>544</amountPlanned>
<wastePlanned>60</wastePlanned>
<amountProduced>169</amountProduced>
<wasteProduced>69</wasteProduced>
<deviceId>114</deviceId>
<types>ConventionalPrinting</types>
<sequenceType>SheetfedPrinting</sequenceType>
<start>2021-05-05T09:46:06-05:00</start>
<end>2021-05-05T09:48:38-05:00</end>
<startPlanned>2021-05-05T07:52:22-05:00</startPlanned>
<endPlanned />
<setuptimePlanned>0</setuptimePlanned>
<prodtimePlanned>0</prodtimePlanned>
<actualTimes />
</worksteps>
<worksteps>
<id>_210505_073301714_027146</id>
<name>Sheet_2 4/4</name>
<job>
<id>060671-21</id>
<name>2021 Laramie High School</name>
</job>
<status>WAITING</status>
<amountPlanned>0</amountPlanned>
<wastePlanned>0</wastePlanned>
<amountProduced>0</amountProduced>
<wasteProduced>0</wasteProduced>
<deviceId>XL106_Pool</deviceId>
<types>ConventionalPrinting</types>
<sequenceType>SheetfedPrinting</sequenceType>
<start />
<end />
<startPlanned />
<endPlanned />
<setuptimePlanned>0</setuptimePlanned>
<prodtimePlanned>0</prodtimePlanned>
<actualTimes />
</worksteps>
</job_worksteps>
--