Traversing JSON objects with tSQL - OPENJSON - sql

I have the following JSON code. I pull the values into a SQL database using OPENJSON, but I am having trouble with the path for the Refund object.
I am trying to pull the "amount" value in the "transactions" object (so the expected value should be 298.47).
SQL code (currently returns only null values)
OPEN(json)
WITH(
OtherJSONstuff varchar '$otherjsonstuff'
Refund int '$.refund[0].transactions.amount' <what should this be
JSON Code
"otherjsonstuff": othervalues
"otherjsonstuff": othervalues
"object": [
{
"id": 212,
"items": [
{
"id": 151,
"quantity": 3,
"item_id": 926,
"subtotal": 30.0,
"tax": 0.0,
"item": {
"id": 926,
"quantity": 3,
"price": "10.00",
"product_id": 934,
"properties": [],
"discount": "0.00",
"tax": []
}
}
],
"action": [
{
"id": 537,
"amount": "298.47", --this is the line I need
"kind": "refund",
"created": "2016-12-13",
"location_id": null,
"parent_id": 537,
}
],
}
],

Having reformatted the JSON code,
it should be $.refund[0].transactions[0].amount
Depend on the array object you want to access, just increment and decrements the values. The root cause is understanding the JSON hierarchy and JNevil has provided you some good resources

Related

Adobe Analytics - how to get report API 2.0 to get multi-level breakdown data using Java

I need help in getting adobe-analytics data when multiple IDs are passed for multi-level breakdown using API 2.0.
I am getting first level data for V124 ----
"metricContainer": {
"metrics": [
{
"columnId": "0",
"id": "metrics/event113",
"sort": "desc"
}
]
},
"dimension": "variables/evar124",
but i want to use IDs returned in above call response to in second level breakdown of v124 to get v125 as---
"metricContainer": {
"metrics": [
{
"columnId": "0",
"id": "metrics/event113",
"sort": "desc",
"filters": [
"0"
]
}
],
"metricFilters": [
{
"id": "0",
"type": "breakdown",
"dimension": "variables/evar124",
"itemId": "2629267831"
},
{
"id": "1",
"type": "breakdown",
"dimension": "variables/evar124",
"itemId": "2629267832"
}
]
},
"dimension": "variables/evar125",
This always returns data only for 2629267831 ID and not 2629267832.
I want to get data for thousands of IDs (returned from first API call) in a single API call. What am i doing wrong?

Querying/flattening multi-level nested JSON in Cosmos DB when array name is random on one level deep down

I would like to get some help to write a query to get a flat JSON to feed Power BI.
The data store is a Cosmos DB container with JSON. The structure is fixed.
The example JSON stucture is this:
"orderid": "999888777",
"name": "dwayne.johnson",
"mainData": {
"giftData": {
"name": "Rocketship",
"giftDetails": {
"**123456789**": {
"boxInfo": [
{
"name": "Leonardo Da Vinci Big Box",
"city": "London",
"country": "United Kingdom",
"currency": "EUR",
"price": 99.43,
}
],
"colorScheme": [
{
"name": "Red Apple Delight",
"code": "123AAA",
}
],
"CourierInfo": [
{
"name": "UPS",
"code": "BBBA333",
}
],
"orderid": "444333434",
"name": "chris.rock",
"mainData": {
"giftData": {
"name": "Junglemaster XXL",
"giftDetails": {
"**32432423424**": {
"boxInfo": [
{
"name": "Gerba Tool Big O",
"city": "München",
"country": "Germany",
"currency": "EUR",
"price": 103.22,
}
],
"colorScheme": [
{
"name": "Green Mix",
"code": "999DDD",
}
],
"CourierInfo": [
{
"name": "GLS",
"code": "CCCGV3",
}
],
I would like to get to following output:
orderid
currency*
price**
999888777
EUR
99.43
444333434
EUR
103.22
*The path would be giftData.giftDetails.123456789 or 32432423424 or any random.boxInfo.currency
**The path would be giftData.giftDetails.123456789 or 32432423424 or any random.boxInfo.price
The question is how can I select the currency and price info 4 levels deep, when the container array name is a random number (in bold)?
Thank you very much for your time and help!

Json_value and Json_query cannot find specified path

I am trying to parse data from the following JSON. I know there are differences between JSON_VALUE and JSON_QUERY but I am trying to just get the path structure correct. I am using the STRICT option to validate my path's and so far other then string$ for JSON Query everything fails by not finding the path. As soon as I add the .data.taskData the path seems to blow up. Any help would be greatly appreciated.
I am setting the following JSON to #json
declare #json nvarchar(max)
SELECT JSON_VALUE(#json, 'strict$.data.taskData.startedLocation') as json
select JSON_QUERY(#json, 'strict$.data.taskData.startedLocation') as json
Below is the JSON I am trying to parse
{"data.taskData":{"startedAtUtc":"2019-08-28T20:21:29.025Z","startedLocation":{"lat":60.7348366,"lon":-124.9856841},"additionalData":[],"bols":[{"number":"1234","product":{"id":"COFFEE","description":"GROUND COFFE 5LB CAN","plannedQuantity":1352,"uom":"PCS","supplier":"WALMART ","accountOf":"","class":"UNKNOWN","loadedQuantity":6600,"netQuantity":9993},"net":"9993"}],"compartments":[{"id":"1","capacity":3400,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"1","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"1000","bol":"1234"}],"loadedQuantity":1000,"productID":"COFFEE"},{"id":"2","capacity":2000,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"2","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"2000","bol":"1234"}],"loadedQuantity":2000,"productID":"COFFEE"},{"id":"3","capacity":1100,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"3","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"1100","bol":"1234"}],"loadedQuantity":1100,"productID":"COFFEE"},{"id":"4","capacity":2700,"commodity":null,"consignee":null,"plannedQuantity":0,"tankID":"4","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"2500","bol":"1234"}],"loadedQuantity":2500,"productID":"COFFEE"}],"detention":{"minutes":null,"reasonCode":null,"notes":null},"initialCompartments":[{"id":"1","capacity":3400,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"1"},{"id":"2","capacity":2000,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"2"},{"id":"3","capacity":1100,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"3"},{"id":"4","capacity":2700,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"4"}],"loadingComplete":"yes","loadingCompleteTime":"2019-08-28T20:23:05.453Z","uom":{"key":"PCS","category":"volume","shortDisplay":"p","longDisplay":"Pieces","conversionFactors":{"gal":0.0625,"L":0.2365882365,"c":4.2267528377}},"variances":[],"completedAtUtc":"2019-08-28T20:23:06.703Z","completedLocation":{"lat":61.7348308,"lon":-124.9856879},"finalCompartments":[{"id":"1","capacity":3400,"productID":"COFFEE","loadedQuantity":1000,"consignee":"KSUAC","tankID":"1"},{"id":"2","capacity":2000,"productID":"COFFEE","loadedQuantity":2000,"consignee":"KSUAC","tankID":"2"},{"id":"3","capacity":1100,"productID":"COFFEE","loadedQuantity":1100,"consignee":"KSUAC","tankID":"3"},{"id":"4","capacity":2700,"productID":"COFFEE","loadedQuantity":2500,"consignee":null,"tankID":"4"}]}}
Try this out. After the code I'll comment about the specialities:
DECLARE #json NVARCHAR(MAX)=N'{
"data.taskData": {
"startedAtUtc": "2019-08-28T20:21:29.025Z",
"startedLocation": {
"lat": 60.7348366,
"lon": -124.9856841
},
"additionalData": [],
"bols": [
{
"number": "1234",
"product": {
"id": "COFFEE",
"description": "GROUND COFFE 5LB CAN",
"plannedQuantity": 1352,
"uom": "PCS",
"supplier": "WALMART ",
"accountOf": "",
"class": "UNKNOWN",
"loadedQuantity": 6600,
"netQuantity": 9993
},
"net": "9993"
}
],
"compartments": [
{
"id": "1",
"capacity": 3400,
"commodity": null,
"consignee": "KSUAC",
"plannedQuantity": 0,
"tankID": "1",
"additionalData": [],
"allLoadsValid": true,
"complete": true,
"error": false,
"locked": false,
"loads": [
{
"isFirst": true,
"quantity": "1000",
"bol": "1234"
}
],
"loadedQuantity": 1000,
"productID": "COFFEE"
},
{
"id": "2",
"capacity": 2000,
"commodity": null,
"consignee": "KSUAC",
"plannedQuantity": 0,
"tankID": "2",
"additionalData": [],
"allLoadsValid": true,
"complete": true,
"error": false,
"locked": false,
"loads": [
{
"isFirst": true,
"quantity": "2000",
"bol": "1234"
}
],
"loadedQuantity": 2000,
"productID": "COFFEE"
},
{
"id": "3",
"capacity": 1100,
"commodity": null,
"consignee": "KSUAC",
"plannedQuantity": 0,
"tankID": "3",
"additionalData": [],
"allLoadsValid": true,
"complete": true,
"error": false,
"locked": false,
"loads": [
{
"isFirst": true,
"quantity": "1100",
"bol": "1234"
}
],
"loadedQuantity": 1100,
"productID": "COFFEE"
},
{
"id": "4",
"capacity": 2700,
"commodity": null,
"consignee": null,
"plannedQuantity": 0,
"tankID": "4",
"additionalData": [],
"allLoadsValid": true,
"complete": true,
"error": false,
"locked": false,
"loads": [
{
"isFirst": true,
"quantity": "2500",
"bol": "1234"
}
],
"loadedQuantity": 2500,
"productID": "COFFEE"
}
],
"detention": {
"minutes": null,
"reasonCode": null,
"notes": null
},
"initialCompartments": [
{
"id": "1",
"capacity": 3400,
"commodity": null,
"consignee": null,
"plannedQuantity": null,
"tankID": "1"
},
{
"id": "2",
"capacity": 2000,
"commodity": null,
"consignee": null,
"plannedQuantity": null,
"tankID": "2"
},
{
"id": "3",
"capacity": 1100,
"commodity": null,
"consignee": null,
"plannedQuantity": null,
"tankID": "3"
},
{
"id": "4",
"capacity": 2700,
"commodity": null,
"consignee": null,
"plannedQuantity": null,
"tankID": "4"
}
],
"loadingComplete": "yes",
"loadingCompleteTime": "2019-08-28T20:23:05.453Z",
"uom": {
"key": "PCS",
"category": "volume",
"shortDisplay": "p",
"longDisplay": "Pieces",
"conversionFactors": {
"gal": 0.0625,
"L": 0.2365882365,
"c": 4.2267528377
}
},
"variances": [],
"completedAtUtc": "2019-08-28T20:23:06.703Z",
"completedLocation": {
"lat": 61.7348308,
"lon": -124.9856879
},
"finalCompartments": [
{
"id": "1",
"capacity": 3400,
"productID": "COFFEE",
"loadedQuantity": 1000,
"consignee": "KSUAC",
"tankID": "1"
},
{
"id": "2",
"capacity": 2000,
"productID": "COFFEE",
"loadedQuantity": 2000,
"consignee": "KSUAC",
"tankID": "2"
},
{
"id": "3",
"capacity": 1100,
"productID": "COFFEE",
"loadedQuantity": 1100,
"consignee": "KSUAC",
"tankID": "3"
},
{
"id": "4",
"capacity": 2700,
"productID": "COFFEE",
"loadedQuantity": 2500,
"consignee": null,
"tankID": "4"
}
]
}
}';
--The query
SELECT FirstLevel.StartedAtUtc
,JSON_VALUE(FirstLevel.startedLocation,'$.lat') AS startedLocation_Lat
,JSON_VALUE(FirstLevel.startedLocation,'$.lon') AS startedLocation_Lon
,FirstLevel.additionalData
,FirstLevel.bols
,Sub_Compartments.id
,Sub_Compartments.capacity
,FirstLevel.loadingComplete
FROM OPENJSON(#json,'$."data.taskData"')
WITH(startedAtUtc DATETIME2
,startedLocation NVARCHAR(MAX) AS JSON
,additionalData NVARCHAR(MAX) AS JSON
,bols NVARCHAR(MAX) AS JSON
--compartments seems to be a 1:n related node
,compartments NVARCHAR(MAX) AS JSON
,loadingComplete NVARCHAR(10)
--and more
) FirstLevel
OUTER APPLY OPENJSON(FirstLevel.compartments)
WITH (id INT
,capacity INT
--more columns
) Sub_Compartments;
The idea in short:
Your JSON is a fully blown deeply nested structure with various data. Returnin the whole and everything would lead to a very redundant flat table. It will be a good idea to query this JSON with a question as narrow as possible.
As you were told already we can use one of these:
JSON_VALUE() to retrieve a scalar value from a given path. Nested JSON will be returned as a string type.
JSON_QUERY() to extract a fragment of the JSON and proceed with it as JSON
OPENJSON is needed to dive into a JSON with repeated elements in order to return the fragments as a derived set row-by-row. Very important is the WITH-clause, which allows to change OPENJSON's default output to a side-by-side of columns (similar to PIVOT).
In this case we can use OPENJSON to dive into the first level and return the objects found there. Some of them are scalar values and can be returned as a typed value, others are JSON objects. In this case we have to use NVARCHAR(MAX) as data type and we must sepcify AS JSON in order to proceed with the return values.
In your JSON the compartments are a 1:n related set. We can use a cascade of OPENJSON calls, using the fragment returned by the first as input and use another WITH-clause to extract the compartement's columns.
I hope, that this exampe will give you enough hints, that you can query any place of your JSON. Good luck!
This works:
select JSON_QUERY(#json, 'strict$."data.taskData".startedLocation') as json
SELECT JSON_VALUE(#json, 'strict$."data.taskData".startedLocation.lat') as json
You need to consider the following:
When you want to extract JSON object or scalar value and your path begins with a dollar sign $ or has special characters in the keys, you need to surround it with quotes ""
Function JSON_QUERY extracts an object or an array from a JSON string. If the value is not an object or an array, the result is NULL in lax mode and an error in strict mode.
Function JSON_VALUE extracts a scalar value from a JSON string. If the path points to not a scalar value, the result is NULL in lax mode and an error in strict mode
When you want to parse JSON string and get results as table, use OPENJSON table-valued function.
With your sample data, you may try the following example:
DECLARE #json nvarchar(max) = N'{"data.taskData":{"startedAtUtc":"2019-08-28T20:21:29.025Z","startedLocation":{"lat":60.7348366,"lon":-124.9856841},"additionalData":[],"bols":[{"number":"1234","product":{"id":"COFFEE","description":"GROUND COFFE 5LB CAN","plannedQuantity":1352,"uom":"PCS","supplier":"WALMART ","accountOf":"","class":"UNKNOWN","loadedQuantity":6600,"netQuantity":9993},"net":"9993"}],"compartments":[{"id":"1","capacity":3400,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"1","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"1000","bol":"1234"}],"loadedQuantity":1000,"productID":"COFFEE"},{"id":"2","capacity":2000,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"2","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"2000","bol":"1234"}],"loadedQuantity":2000,"productID":"COFFEE"},{"id":"3","capacity":1100,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"3","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"1100","bol":"1234"}],"loadedQuantity":1100,"productID":"COFFEE"},{"id":"4","capacity":2700,"commodity":null,"consignee":null,"plannedQuantity":0,"tankID":"4","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"2500","bol":"1234"}],"loadedQuantity":2500,"productID":"COFFEE"}],"detention":{"minutes":null,"reasonCode":null,"notes":null},"initialCompartments":[{"id":"1","capacity":3400,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"1"},{"id":"2","capacity":2000,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"2"},{"id":"3","capacity":1100,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"3"},{"id":"4","capacity":2700,"commodity":null,"consignee":null,"plannedQuantity":null,"tankID":"4"}],"loadingComplete":"yes","loadingCompleteTime":"2019-08-28T20:23:05.453Z","uom":{"key":"PCS","category":"volume","shortDisplay":"p","longDisplay":"Pieces","conversionFactors":{"gal":0.0625,"L":0.2365882365,"c":4.2267528377}},"variances":[],"completedAtUtc":"2019-08-28T20:23:06.703Z","completedLocation":{"lat":61.7348308,"lon":-124.9856879},"finalCompartments":[{"id":"1","capacity":3400,"productID":"COFFEE","loadedQuantity":1000,"consignee":"KSUAC","tankID":"1"},{"id":"2","capacity":2000,"productID":"COFFEE","loadedQuantity":2000,"consignee":"KSUAC","tankID":"2"},{"id":"3","capacity":1100,"productID":"COFFEE","loadedQuantity":1100,"consignee":"KSUAC","tankID":"3"},{"id":"4","capacity":2700,"productID":"COFFEE","loadedQuantity":2500,"consignee":null,"tankID":"4"}]}}'
SELECT
JSON_QUERY(#json, 'strict $."data.taskData".startedLocation') AS StartedLocation,
JSON_VALUE(#json, 'strict $."data.taskData".startedLocation.lat') as Lat,
JSON_VALUE(#json, 'strict $."data.taskData".startedLocation.lon') as Lon
SELECT *
FROM OPENJSON(#json, 'strict $."data.taskData".compartments') AS Compartments
Output:
----------------------------------------------------------------
StartedLocation Lat Lon
----------------------------------------------------------------
{"lat":60.7348366,"lon":-124.9856841} 60.7348366 -124.9856841
----------------
key value type
----------------
0 {"id":"1","capacity":3400,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"1","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"1000","bol":"1234"}],"loadedQuantity":1000,"productID":"COFFEE"} 5
1 {"id":"2","capacity":2000,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"2","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"2000","bol":"1234"}],"loadedQuantity":2000,"productID":"COFFEE"} 5
2 {"id":"3","capacity":1100,"commodity":null,"consignee":"KSUAC","plannedQuantity":0,"tankID":"3","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"1100","bol":"1234"}],"loadedQuantity":1100,"productID":"COFFEE"} 5
3 {"id":"4","capacity":2700,"commodity":null,"consignee":null,"plannedQuantity":0,"tankID":"4","additionalData":[],"allLoadsValid":true,"complete":true,"error":false,"locked":false,"loads":[{"isFirst":true,"quantity":"2500","bol":"1234"}],"loadedQuantity":2500,"productID":"COFFEE"} 5

How to filter the Array of Dictionary if the dictionary elements are null?

I need to filter the array of dictionary , but in my structure some of the values are null
"data": [
{
"date": "2019-04-12 00:00:00",
"points": 825,
"trips": [
{
"id": 108,
"trip_detail": “<null>”
}
},
{
"date": "2019-04-11 00:00:00",
"points": 2475,
"trips": [
{
"id": 73,
"trip_detail": {
"id": 53,
"score": 8.25,
"points": 825,
}
},
{
"id": 74,
"trip_detail": {
"id": 54,
"score": 8.25,
"points": 825,
}
},
so in the above result I need to remove the first array of dictionary as well as that array.
Use filter to loop over a collection and return an Array containing only those elements that match an include condition.

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