Karate- Error when comparing json objects - json

I am trying to match some parameters with json reposne
my actual response is like
{
"timestamp": 1595994767386,
"country": "MH",
"accessible_device_types": [
{
"name": "ESS Client",
"raw_name": "ABC",
"permission": 7,
"permission_bits": {
"INSTALL_LIMITED_RELEASE_SOFTWARE": true,
"INSTALL_LATEST_SOFTWARE_ONLY": true,
"INSTALL_SOFTWARE": true
}
},
used below statment for comparing:
match response.accessible_device_types contains [{"raw_name": "ABC"}]
Reason for error from report: expected: {raw_name=ABC}, reason: actual value does not contain expected
Looks like comparing without quotes. Why is it taking out the quotes? Any recommendations
How to compare "INSTALL_SOFTWARE": true

2 options:
* def nameAbc = {"raw_name": "ABC"}
* match response.accessible_device_types contains '#(^nameAbc)'
This will work in 0.9.6.RC4 onwards:
* match response.accessible_device_types contains deep {"raw_name": "ABC"}

Related

Sort by nested fields in json in Power Automate

I'm trying to sort a JSON String in Power Automate by a nested field called "orderHint".
My JSON String looks like this:
[
{
"id": "5134",
"value": {
"isChecked": false,
"title": "This is another test",
"orderHint": "8585298133570680672PF"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
},
{
"id": "26576",
"value": {
"isChecked": true,
"title": "This is a test",
"orderHint": "8585498133570680672DE"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
}
]
When I'm trying to sort by "orderHint", I get an error:
"'The template language function 'sort' did not find the named sortField 'orderHint' on one or more objects in the array."
I'm using the following expression:
sort(variables('varArrayChecked'), 'value/orderHint')
Sorting by other fields works fine, e.g.:
sort(variables('varArrayChecked'), 'id')
Is there any way how I can sort by a nested field in a JSON String?
Thanks in advance!
You can use the Advanced Data Operations connector as it will do it for you in a single step.
The Flatten Object Array step is perfect for the payload you've provided.
You can see that it will take the data, flatten it and you have the ability to sort it on the way out (noting that the Array variable contains the exact JSON you provided in your question) ...
Note: Balance Output must be set to true in order for the sorting to occur.
Result
This is the resulting JSON order by orderHint ascending.
[
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
},
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
}
]
... and to show it in descending order (which is obvious, but simply change the sort order object value from Asc to Desc) ...
[
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
},
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
}
]

How to update a specific JSON element inside a complex JSON document in postgresql 10

I have JSON document which is stored under single column of type jsonb inside postgresql which looks like below:
{
"resourceType": "Bundle",
"type": "transaction",
"entry": [
{
"fullUrl": "urn:uuid:100",
"resource": {
"resourceType": "Encounter",
"id": "110",
"status": "planned",
"priority": {
"coding": [
{
"code": "ASAP"
}
]
},
"subject": {
"reference": "Patient/123"
},
"appointment": [
{
"reference": "Appointment/12213#42"
}
],
"diagnosis": [
{
"condition": {
"reference": "Condition/condReferenceValue"
},
"use": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/diagnosis-role",
"code": "AD"
},
{
"system": "http://terminology.hl7.org/CodeSystem/diagnosis-role",
"code": "DD"
}
]
}
}
],
"hospitalization": {
"preAdmissionIdentifier": {
"system": "https://system.html"
}
},
"location": [
{
"location": {
"display": "Mumbai"
},
"status": "active"
},
{
"status": "planned"
}
]
},
"request": {
"method": "POST",
"url": "Encounter"
}
}
]
}
Now, I want to update value for reference under subject attribute. So, I tried below way but it throws an error:
update fhir.testing set names = jsonb_set(names,'{"subject":{"reference"','"Patient/1"',true) where id = 10;
Error:
SQL Error [22P02]: ERROR: malformed array literal: "{"subject":{"reference""
Detail: Unexpected array element.
I referred this link but didn't work out for me. How can I do it?
I don't use Postgres that much but from what i read in the relative jsonb_set example in the documentation of JSON functions (and since you want to update) shouldn't it be
jsonb_set(names, '{entry,0,subject,reference}','Patient/1', false)
instead of
jsonb_set(names,'{"subject":{"reference"','"Patient/1"',true)
jsonb
jsonb_set(target jsonb, path text[], new_value jsonb [, create_missing
boolean])
Returns target with the section designated by path replaced by
new_value, or with new_value added if create_missing is true (default
is true) and the item designated by path does not exist. As with the
path oriented operators, negative integers that appear in path count
from the end of JSON arrays.
EDIT
To explain the path used in jsonb_set, check this example.
jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)
returns
[{"f1":[2,3,4],"f2":null},2,null,3]
As i understand if a sub-element in a complex JSON document is an array, you need to specify it's index e.g. 0,1,2,...
EDIT
Always look very carefully the structure of the JSON document. I simply write this because i did not see that subject was a child of resource and that is causing you the error.
So the correct path is actually '{entry,0,resource,subject,reference}'
Correct Query for your requirement is:
update fhir.testing
set names= jsonb_set(names, '{entry,0,resource,subject,reference}', '"Patient/1"' , false)
where id = 10;
Explanation
json_set takes 4 parameter
target_json (jsonb) - which accept jsonb type data. In your case it is names field.
path (text[]) - which accepts a text array. in your case it is '{entry,0,resource,subject,reference}'.
new_value (jsonb) - in your case you want to change it to '"Patient/1"'.
create_missing (boolean) - in your case it should be false. as you want to replace the existing one. if you want to create the reference with given value in case of not found then just mark it true.
the value is not valid json, try this out:
update fhir.testing set names = jsonb_set(names, '{"entry": [{"resource": {"subject":{"reference":"Patient/1"} }}]}',true) where id = 10;
You have to create a valid json, closing every { and every ], yours was
'{"subject":{"reference"'

Json output format from SQL Server 2017

I have a stored procedure in SQL Server 2017 that outputs a result set as JSON. It works to output the set just fine, but I want it in a different format.
According to the MS documentation, the format it's sending out is as expected, but it seems counter-intuitive to me. I'm not JSON expert by any means but I've always assumed it to be a single object representation of a structure.
The query is:
SELECT
e.EventID AS 'Event.ID',
EventDate AS 'Event.Date',
ea.ActivityID AS 'Event.Activity.ID',
ea.CreateDate AS 'Event.Activity.Date',
ea.Notes AS 'Event.Activity.Notes'
FROM
Events e
JOIN
dbo.EventActivities ea ON e.EventID = ea.EventID
FOR JSON PATH
This returns an output of:
[
{"Event": {
"ID":"236",
"Date":"2019-03-01",
"Activity": {"ID": 10,
"Date":"2019-01-02T11:47:33.2800000",
"Notes":"Event created"}
}
},
{"Event": {
"ID":"236",
"Date":"2019-03-01",
"Activity": {"ID":20,
"Date":"2019-01-02T11:47:34.3933333",
"Notes":"Staff selected"}
}
},
{"Event": {
"ID":"236",
"Date":"2019-03-01",
"Activity": {"ID":20,
"Date":"2019-01-02T11:47:34.3933333",
"Notes":"Staff selected"}
}
}
]
When I format this manually (to visualise it better, it's giving me an array of 3 identical events, for each activity. This is consistent with what MS say in Format Nested JSON
I was expecting (or rather hoping) to see something like:
[
{
"Event": {
"ID": "236",
"Date": "2019-03-01",
"Activity": [
{
"ID": 10,
"Date": "2019-01-02T11:47:33.2800000",
"Notes": "Event created"
},
{
"ID": 20,
"Date": "2019-01-02T11:47:34.3933333",
"Notes": "Staff selected"
},
{
"ID": 20,
"Date": "2019-01-02T11:47:34.3933333",
"Notes": "Staff selected"
}
]
}
}
]
Is it possible to get an output formulated like this? or would this be invalid?
To start, you can test if a JSON String is valid with ISJSON. The expected output you indicated does not pass validation, but is close. It is missing a "[]" for the inner array.
However, I see where you were going with this. To better explain what I think the issue you are running into is, I am going to beautify the format of the output JSON from your query to match your expected JSON.
Original output as follows:
[
{"Event":
{"ID":"236","Date":"2019-03-01",
"Activity":{
"ID":10,"Date":"2019-01-02T11:47:33.2800000","Notes":"Event created"
}
}
},
{"Event":
{"ID":"236","Date":"2019-03-01",
"Activity":{
"ID":20,"Date":"2019-01-02T11:47:34.3933333","Notes":"Staff selected"}
}
},
{"Event":
{"ID":"236","Date":"2019-03-01",
"Activity":{
"ID":20,"Date":"2019-01-02T11:47:34.3933333","Notes":"Staff selected"
}
}
}
]
Based on your ideal format, a possible valid JSON string would be as follows:
{"Event":
[
{"ID":236,"Date":"2019-03-01",
"Activity":
[
{"ID":10,"Date":"2019-01-02T11:47:33.2800000","Notes":"Event created"},
{"ID":20,"Date":"2019-01-02T11:47:34.3933333","Notes":"Staff selected"},
{"ID":20,"Date":"2019-01-02T11:47:34.3933333","Notes":"Staff selected"}
]
}
]
}
You can achieve this by adjusting your table alias for the second table and using FOR JSON AUTO and ROOT. It will return an output with the "Event" attributes not repeated for each of its "EventActivities". For each "Event" it will put its related "EventActivities" into an array instead.
The following SQL will return the desired output:
SELECT [Event].EventID AS 'ID',
[Event].EventDate AS 'Date',
Activity.ActivityID AS 'ID',
Activity.CreateDate AS 'Date',
Activity.Notes AS 'Notes'
FROM #Events [Event]
JOIN #EventActivities Activity
ON [Event].EventID = Activity.EventID
FOR JSON AUTO, ROOT('Event')
The exact output for this will be as follows:
{"Event":[{"ID":236,"Date":"2019-03-01","Activity":[{"ID":10,"Date":"2019-01-02T11:47:33.2800000Z","Notes":"Event created"},{"ID":20,"Date":"2019-01-02T11:47:33.2800000Z","Notes":"Staff selected"},{"ID":20,"Date":"2019-01-02T11:47:33.2800000Z","Notes":"Staff selected"}]}]}
It will not return in a beautified format, but spaces and indentation can be added without compromising the fact that it is valid JSON, while also achieving the intended array requirements.
Your expected output is not a valid JSON. But, assuming you are referring Activity field as an array of activities, you can use nested queries.
SELECT
E.EventID AS 'Event.ID',
E.EventDate AS 'Event.Date',
(
SELECT
A.ActivityID AS 'ID',
A.CreateDate AS 'Date',
A.Notes AS 'Notes'
FROM dbo.EventActivities AS A
WHERE A.EventID = E.EventID
FOR JSON PATH
) AS 'Event.Activities'
FROM dbo.Events AS E
FOR JSON PATH

How to read if the UUID format from a json file?

I have a json file say file.json
It has a json body like this
{
"input": {
"url": "/abc/def/efg/{UUID}.txt",
"type": "text"
},
"output": {
"result": 10,
"content": ""
}
}
{UUID} is any valid UUID.
How do I allow any UUID as regex / generic form in place {UUID} ??
You can use this regex to find and match the 'GUID' part or your text:
/\"url\":\s?\"[^{]+\{([^}]+)/
The regex matches '"url": "' at start, then matches up to '{' before it creates a Group where it stores the 'UUID'
You get an array as result, where you should grap Group 1.
How to use:
if your text is in a variable called 'text', then you can do:
var UUID = text.match(/\"url\":\s?\"[^{]+\{([^}]+)/)[1];

S3 to Redshift: Unknown boolean format

Executing a copy command from S3 to Redshift, loading JSON files. I have some fields as bool in a new table I'm inserting into and always getting the following error: "Unknown boolean format"
My JSON is well parsed, ran a million tests on that already. I've tried passing in the boolean fields as:
false // "false" // "False" // 0 // "0" // null
But always get the same error, when executing:
select * from stl_load_errors;
err_code err_reason
1210 Unknown boolean format
I've seen some comments about using IGNOREHEADER in my statement but that isn't an option because the files I'm dealing with are in a single row json format. Ignoring the head would basically mean not reading thefile at all. I have other tables working like this and work fine, but don't have any bool columns in those tables.
The COPY from JSON Format documentation page provides an example that includes a Boolean:
{
"id": 0,
"guid": "84512477-fa49-456b-b407-581d0d851c3c",
"isActive": true,
"tags": [
"nisi",
"culpa",
"ad",
"amet",
"voluptate",
"reprehenderit",
"veniam"
],
"friends": [
{
"id": 0,
"name": "Carmella Gonzales"
},
{
"id": 1,
"name": "Renaldo"
}
]
}
The Boolean documentation page shows values similar to what you have tried.