Powershell Json object from object - sql

I have a method that returns a Json object from a query using the FOR JOSN PATH approach.
Example:
SELECT State, COUNT(*) AS Items
FROM dbo.States
WHERE SomeFilterColumn = #FilterParam
GROUP BY State
FOR JSON PATH
This returns
[
{
"State": "Pending",
"Items": 23
},
{
"State": "Finished",
"Items", 7736
}
]
I also have a function in Powershell that returns the result. However I have issues how it is returned.
function Get-Json-From-Query {
param(
[Int32]$filterParam
)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
...
$SqlCmd = $SqlConnection.CreateCommand();
$SqlCmd.CommandText = $Query #The query shown above
$SqlCmd.Parameters.Add("#FilterParam", $filterParam);
$SqlCmd.ExecuteScalar()
}
However I do not know how to get the result of my query properly added to a JSON value.
I tried many things like:
#{
States = (Get-Json-From-Query -filterParam 1234)
}
However this gives me also the param information which I do not want:
Expecting
{ "States": [... The Array show above ... ] }
Actual
{
"States": [
{
"CompareInfo": 0,
"XmlSchemaCollectionDatabase": "",
"XmlSchemaCollectionOwningSchema": "",
"XmlSchemaCollectionName": "",
"ForceColumnEncryption": false,
"DbType": 11,
"LocaleId": 0,
"ParameterName": "#FilterParam",
"Precision": 0,
"Scale": 0,
"SqlDbType": 8,
"SqlValue": "1234",
"UdtTypeName": "",
"TypeName": "",
"Value": 1234,
"Direction": 1,
"IsNullable": false,
"Offset": 0,
"Size": 0,
"SourceColumn": "",
"SourceColumnNullMapping": false,
"SourceVersion": 512
},
"[{\"State\":\"Pending\",\"Items\":23},{\"State\":\"Finished\",\"Items\":7763}]"
]
}
How do I get the right data in my Json object?

Related

Dataweave 2 - How can i join 2 arrays by index inside object without lose data parent object

How to join 3 arrays by index (lines, pckSlip and linesDt) and generate an arrays object by linesDt after that you have to generate a new field "totalCost" which is to add the "cost" field of all cost elements in the linesDt array, note the "number" field in the original object is preserved for all new elements spawned from the parent.
Input:
{
"lines":[
{
"requestedQuantity":1,
"pendingQuantity":0
},
{
"requestedQuantity":2,
"pendingQuantity":0
}
],
"number":"98",
"pckSlip":[
{
"trackingNumber":"10534",
"boxesNum":1
},
{
"trackingNumber":"00049",
"boxesNum":1
}
],
"linesDt":[
{
"number":"5678",
"cost":7.7
},
{
"number":"1234",
"cost":7.3
}
]
}
Ouput:
[
{
"number":"5678",
"cost":7.7,
"requestedQuantity":1,
"pendingQuantity":0,
"trackingNumber":"10534",
"boxesNum":1,
"totalCost":15,
"order":"98"
},
{
"number":"1234",
"cost":7.3,
"requestedQuantity":2,
"pendingQuantity":0,
"trackingNumber":"00049",
"boxesNum":1,
"totalCost":15,
"order":"98"
}
]
NOTE: We generate 2 new elements because they are the total of indices found in "linesDt" within an array of elements.
Any help would be appreciated. Thank you.
Mapping each element of lines gives you the index to use in the other arrays. The ++ operator can be used to concatenate objects all the objects together. The calculated fields are added just as another object.
%dw 2.0
output application/json
var totalCost = sum(payload.linesDt.*cost)
---
payload.lines map (
$
++ payload.pckSlip[$$]
++ payload.linesDt[$$]
++ {totalCost: totalCost, order: payload.number}
)
Output:
[
{
"requestedQuantity": 1,
"pendingQuantity": 0,
"trackingNumber": "10534",
"boxesNum": 1,
"number": "5678",
"cost": 7.7,
"totalCost": 15.0,
"order": "98"
},
{
"requestedQuantity": 2,
"pendingQuantity": 0,
"trackingNumber": "00049",
"boxesNum": 1,
"number": "1234",
"cost": 7.3,
"totalCost": 15.0,
"order": "98"
}
]
Assuming that the size of each of the arrays is going to be the same.
Script
%dw 2.0
output application/json
---
1 to sizeOf(payload.lines) map {
(payload.linesDt[($$)] ++ payload.lines[($$)] ++ payload.pckSlip[($$)] ++ ("totalCost": sum(payload.linesDt..cost) as String) ++ ("order": payload.number))
}
Output
[
{
"number": "5678",
"cost": 7.7,
"requestedQuantity": 1,
"pendingQuantity": 0,
"trackingNumber": "10534",
"boxesNum": 1,
"totalCost": "15",
"order": "98"
},
{
"number": "1234",
"cost": 7.3,
"requestedQuantity": 2,
"pendingQuantity": 0,
"trackingNumber": "00049",
"boxesNum": 1,
"totalCost": "15",
"order": "98"
}
]

Postgresql and jsonb - inserting a key/value into a multi-level array

Very similar to this post, but I struggle to adapt from their solution..
My table : public.challenge, column lines JSONB
My initial JSON in lines :
[
{
"line": 1,
"blocs": [
{
"size": 100,
"name": "abc"
},
{
"size": 100,
"name": "def"
},
{
"size": 100,
"name": "ghi"
}
]
},
{
"line": 2,
"blocs": [
{
"size": 100,
"name": "xyz"
}
]
}
]
Desired update :
[
{
"line": 1,
"blocs": [
{
"size": 100,
"name": "abc",
"type": "regular"
},
{
"size": 100,
"name": "def",
"type": "regular"
},
{
"size": 100,
"name": "ghi",
"type": "regular"
}
]
},
{
"line": 2,
"blocs": [
{
"size": 100,
"name": "xyz",
"type": "regular"
}
]
}
]
So basically I need to add the type key+value in every object of blocs, for each element of the root array.
My unsuccessful attempt looks like this :
UPDATE public.challenge SET lines = jsonb_set(lines, '{}', (
SELECT jsonb_set(line, '{blocs}', (
SELECT jsonb_agg( bloc || '{"type":"regular"}' )
FROM jsonb_array_elements(line->'{blocs}') bloc
))
FROM jsonb_array_elements(lines) line
))
;
(currently it sets the whole column as null, maybe due to jsonb_set(lines, '{}' while my json begins as an array ?)
Thanks!
Use jsonb_array_elements to unnest all the array elements and then add the required json and use jsonb_agg to aggregate it again:
with cte as
(select id,
jsonb_agg(jsonb_set(val1,
'{blocs}',
(select jsonb_agg(arr2 || '{"type": "regular"}')
from jsonb_array_elements(arr1.val1 - >
'blocs') arr2)))
from challenge,
jsonb_array_elements(lines) arr1(val1)
group by 1)
update challenge
set lines = (cte.jsonb_agg)
from cte
where challenge.id = cte.id
DEMO

How to find dynamic key values in Karate?

I am hitting JIRA API to fetch cycle id based on cycle name
API : http://localhost:8080/rest/zapi/latest/cycle?projectId=78654&versionId=123
and I am getting following response:
{
"1345": {
"totalExecutions": 0,
"endDate": "",
"description": "",
"versionName": "Unscheduled",
"projectKey": "ABC",
"totalDefects": 0,
"versionId": 123,
"name": "First cycle",
"totalFolders": 0,
"projectId": 78654
},
"5789": {
"totalExecutions": 0,
"endDate": "",
"description": "",
"versionName": "Unscheduled",
"projectKey": "ABC",
"totalDefects": 0,
"versionId": 123,
"name": "Karate DEMO",
"totalFolders": 0,
"projectId": 78654
},
"6543": {
"totalExecutions": 0,
"endDate": "",
"description": "",
"versionName": "Unscheduled",
"projectKey": "ABC",
"totalDefects": 0,
"versionId": 123,
"name": "Second Cycle",
"totalFolders": 0,
"projectId": 78654
},
"recordsCount": 3
}
Here Id's are dynamic i.e. 1345,5789,6543
How do I fetch Id i.e. 5789 where name is "Karate DEMO" using karate jsonpath
Use a JSON transform to change the shape which makes it easier to do JsonPath. You can also find data because karate.forEach() is a "scan": https://github.com/intuit/karate#json-transforms
* def list = []
* def fun = function(k, v){ karate.appendTo('list', { key: k, val: v } )}
* karate.forEach(response, fun)
* def keys = $list[?(#.val.name=='Karate DEMO')].key

How to retrieve automated test cases result using CAAC/Rally API

I'm using this URL in REST:
https://rally1.rallydev.com/slm/webservice/v2.0/testcases?query=((Project.Name = "Team 2 - OCEANS Change Management") AND (Method = "Automated"))fetch=type,formattedId,Method,Owner
It is throwing this error:
{
"QueryResult": {
"_rallyAPIMajor": "2",
"_rallyAPIMinor": "0",
"Errors": [
"Could not parse: Cannot parse expression \"((Project.Name = \"Team 2 - OCEANS Change Management\") AND (Method = \"Automated\"))fetch=type,formattedId,Method,Owner\" as a query"
],
"Warnings": [],
"TotalResultCount": 0,
"StartIndex": 0,
"PageSize": 0,
"Results": []
}
}
Perhaps the string "fetch" should be "&fetch"?

how to write a query in oracle to get a single value as string from the json list?

I have a json string containing list of data, I want to write a query to get a single value based on the condition. but it is returning list of values. please help me to write a valid query in oracle database.
My json string looks like
[
{
"Key": [
{
"obj": {
"xyz":"cdf"
},
"Info": [
{
"Code": "",
"tax": "",
"rate": "",
"taxAmount": {
"formattedAmount": "",
"Amount": ""
}
},
{
"Code": "qwer",
"tax": "ggs",
"rate": "0",
"taxAmount": {
"formattedAmount": "10.00",
"Amount": "10.00"
},
"key": "qwer"
},
{
"Code": "poiu",
"tax": "ggs",
"rate": "0",
"taxAmount": {
"formattedAmount": "20.00",
"Amount": "20.00"
},
"key": "poiu"
},
{
"coverageCode": "zxcv",
"tax": "ggs",
"rate": "0",
"taxAmount": {
"formattedAmount": "30.00",
"Amount": "30.00"
},
"key": "zxcv"
}
]
},
{
"status": "S"
}
]
}
]
I want to get formattedAmount value "10.00". Written a query like
SELECT json_query(details, '$.Info.taxAmount.formattedAmount' WITH WRAPPER)
FROM details_table where json_query(details, '$.Info.Code' WITH WRAPPER) = 'qwer';
returns no value. without where clause i'll get all the formattedAmont in list [,"10.00","20.00","30.00"]