SQL Server : SELECT JSON Column in this JSON Structure - sql-server

I would like to know will it possible to select data from below JSON structure?
[
{
"A": 6,
"Status": 1
},
{
"A": 3,
"Status": 0
},
{
"A": 6,
"Status": 1
},
{
"A": 7,
"Status": 0
}
]
According this link, there is Property before the array/object.
"EmployeeInfo": {
"FirstName":"Jignesh",
"LastName":"Trivedi",
"Code":"CCEEDD",
"Addresses": [
{ "Address":"Test 0", "City":"Gandhinagar", "State":"Gujarat"},
{ "Address":"Test 1", "City":"Gandhinagar", "State":"Gujarat"}
]
}
For example, (getting sample from above link), we see the query is started with property EmployeeInfo so that make query possible to get data in this query.
SELECT JSON_VALUE(#JSONData, '$.EmployeeInfo.FirstName')
So I just can't figure out how could this be achieve from the structure provide above, anyone could point me to some sample code that will be helpful. Thanks.

You have two options to parse this JSON array:
Using OPENJSON() with explicit schema once - to get the content of each item
Using OPENJSON() twice - to get the index and the content of each item
JSON:
DECLARE #json varchar(max) = '
[
{
"A": 6,
"Status": 1
},
{
"A": 3,
"Status": 0
},
{
"A": 6,
"Status": 1
},
{
"A": 7,
"Status": 0
}
]'
Using OPENJSON() with explicit schema once:
SELECT A, Status
FROM OPENJSON(#json) WITH (
A int,
Status int
)
Result:
A Status
6 1
3 0
6 1
7 0
Using OPENJSON() twice:
SELECT
j1.[key] AS Index,
j2.A, j2.Status
FROM OPENJSON(#json) j1
CROSS APPLY OPENJSON(j1.[value]) WITH (
A int,
Status int
) j2
Result:
Index A Status
0 6 1
1 3 0
2 6 1
3 7 0
Of course, you can always access an array item by index:
SELECT
JSON_QUERY(#json, '$[0]') AS Item,
JSON_VALUE(#json, '$[0].A') AS A,
JSON_VALUE(#json, '$[0].Status') AS Status
Result:
Item A Status
{"A": 6, "Status": 1} 6 1

Something like this
declare #json nvarchar(max) =N'
[
{
"A": 6,
"Status": 1
},
{
"A": 3,
"Status": 0
},
{
"A": 6,
"Status": 1
},
{
"A": 7,
"Status": 0
}
]'
select * from openjson(#json) with (A int,
Status int);
Output
A Status
6 1
3 0
6 1
7 0

Related

T-SQL FOR JSON PATH but without a column name in the JSON result

Is there a way in SQL to return an array of values without the column name included? I've searched for a solution but have not found anything
CREATE TABLE #data (
id int
);
INSERT INTO #data VALUES (1)
INSERT INTO #data VALUES (2)
INSERT INTO #data VALUES (3)
INSERT INTO #data VALUES (4)
INSERT INTO #data VALUES (5)
INSERT INTO #data VALUES (6)
INSERT INTO #data VALUES (7)
INSERT INTO #data VALUES (8)
INSERT INTO #data VALUES (9)
INSERT INTO #data VALUES (10)
SELECT id FROM #data FOR JSON PATH
DROP TABLE #data
The above T-SQL returns the below JSON but I want it without the column name just an array of values
[
{
"id": 1
},
{
"id": 2
},
{
"id": 3
},
{
"id": 4
},
{
"id": 5
},
{
"id": 6
},
{
"id": 7
},
{
"id": 8
},
{
"id": 9
},
{
"id": 10
}
]
I would like the resulting output to be:
[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]

How would I use the value of one element in an array to access another array in React?

I have data from an API that is like:
[
{
"id": 1,
"name": "eventName1",
"related_events": [
2,
3
]
},
{
"id": 2,
"name": "eventName2",
"related_events": [
1,
3
]
},
{
"id": 3,
"name": "eventName3",
"related_events": [
1,
2,
4
]
}
]
Each element of the related_events array links to an id element. So, if the related_events has 2 and 3 then that means I have to call id 2 and 3.
If I want to get the name that corresponds to the related_events ids, how would I code it?
Example, if the related_events has 2 and 3 then I want to display the names "eventName2" and "eventName3".
You can search your data for the related_event ids by iterating through or by using Array.find function.
Something like this:
const testData = [
{
"id": 1,
"name": "eventName1",
"related_events": [
2,
3
]
},
{
"id": 2,
"name": "eventName2",
"related_events": [
1,
3
]
},
{
"id": 3,
"name": "eventName3",
"related_events": [
1,
2,
4
]
}
];
// the object to test from the data
const testObject = testData[0];
//we iterate over every id of related_events
testObject.related_events.forEach(targetId => {
//we search in the data for a matching element that corresponds to the related_event id
const targetElement = testData.find(element => {return element.id === targetId});
//check if that element actually exists, just to be sure
if (!!targetElement) {
console.log(targetElement.name);
}
else {
console.log("no matching object found");
}
});
There is really no difference when using React to just using basic javascript, as the logic is the same.

Postgresql array to json

I have a table like:
id
time_serie
value
1
2020-09-25 00:00:00
100
1
2020-09-25 00:10:00
200
1
2020-09-25 00:20:00
300
1
2020-09-25 00:30:00
400
I want a JSON output as:
{
"ID": 1,
"time_serie": [
{
"position": 1,
"inQuantity": 100
},
{
"position": 2,
"inQuantity": 200
},
{
"position": 3,
"inQuantity": 300
},
{
"position": 4,
"inQuantity": 400
}
...
]
}
Thanks
You can use a mix of JSON functions along with ROW_NUMBER() window function in order to generate positions such as
SELECT *
FROM
(
SELECT JSON_BUILD_OBJECT('ID', id,
'time_serie',
JSON_AGG(
JSON_BUILD_OBJECT('position',id,'inQuantity',value)
)
)
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY time) AS rn FROM t) AS t
GROUP BY id
) AS j
Demo

How to fetch the changed object from an array in AngularJs?

I have 2 arrays stored in arr1 and arr2
var arr1 = [
{
"status": 4,
"id": 1
},
{
"status": 2,
"id": 2
},
{
"status": 1,
"id": 3
}];
var arr2 = [{
"status": 4,
"id": 1
},
{
"status": 2,
"id": 2
},
{
"status": 4,
"id": 3
}];
Am using angular.equals(arr1, arr2) to check 2 arrays are same but when the status changes in arr2, how can i fetch the respective id from arr2.
This will give you the array which contains the ids of the elements with different status value.
var elementsChanged = arr1.map((val, idx) => {
if (val.status !== arr2[idx].status)
return arr2[idx].id
})
You need to first put arr2 in $scope and then set $watch on your data to detect the exact id change.
$scope.$watch('arr2', function(newValue, oldValue){.//iterate and detect your id change..}, true);
Demo for watcher
Demo Updated with #korte map suggestion.

how to return row if value find in array in postgres

{
actName: null,
applicable: {
applicable: [ 5, 4, 1 ]
},
status: 1,
id: 2
}
{
actName: null,
applicable: {
applicable: [ 3, 2 ]
},
status: 1,
id: 1
}
Is that possible to find value in array, like if i search integer value 2 in applicable array return one row with id 1.
with t(j) as (values
('{
"actName": null,
"applicable": {
"applicable": [ 5, 4, 1 ]
},
"status": 1,
"id": 2
}'::jsonb),
('{
"actName": null,
"applicable": {
"applicable": [ 3, 2 ]
},
"status": 1,
"id": 1
}')
)
select j ->> 'id' as id
from t
where exists (
select 1
from jsonb_array_elements_text(j -> 'applicable' -> 'applicable') s(i)
where i = '2'
)
;
id
----
1
With JSONB's #> you can query for any element by following the structure of your document, for example:
WITH data(d) AS (VALUES
('{
"actName": null,
"applicable": {
"applicable": [ 5, 4, 1 ]
},
"status": 1,
"id": 2
}'::JSONB),
('{
"actName": null,
"applicable": {
"applicable": [ 3, 2 ]
},
"status": 1,
"id": 1
}')
)
SELECT d ->> 'id' AS id
FROM data
WHERE d #> '{"applicable":{"applicable":[1]}}';
Assumption here is that you have jsonb with array of json docs in your database, query may be the following:
WITH test_data AS (
SELECT '[{
"actName": "null",
"applicable": {
"applicable": [5,4,1]
},
"status":1,
"id":2
},
{
"actName": "null",
"applicable": {
"applicable": [3,2]
},
"status": 1,
"id": 1
}]'::JSONB AS jsonb_value
)
SELECT
jsonb_doc->>'id' AS id,
jsonb_doc->'applicable' #>'{applicable}' AS appl_array_result
FROM
test_data td,
jsonb_array_elements(td.jsonb_value) AS jsonb_doc
WHERE (jsonb_doc->'applicable' #>'{applicable}') #> '2'::JSONB;
Output:
id | appl_array_result
----+-------------------
1 | [3, 2]
(1 row)

Resources