Updating nested JSON sub-arrays by various keys - json

I am using SQL Server 2016 version, I can't tell if this is something specific to this version or whether I am missing something. I am trying to update a common property, node_status, inside an array of objects. The SQL update I am running to update node_status where is_node_complete: false is only updating the first index it finds rather than all that fit the query for node_status.
The JSON structure inside column json_doc in some_table
{
"personnel": [
{
"node_id": "FDA64E9F-3BAC-45FA-8819-8A086D96B359",
"node_data": {
"is_approved": null,
"is_node_complete": false,
"node_status": "requested"
}
},
{
"node_id": "AF829232-32F4-464B-8817-50ED24447AA4",
"node_data": {
"is_approved": null,
"is_node_complete": false,
"node_status": "requested"
}
},
{
"node_id": "E18F8197-B16D-4E0B-8EE9-DBF5B23A8EB5",
"node_data": {
"is_approved": true,
"is_node_complete": true,
"node_status": "complete"
}
},
{
"node_id": "286700AE-81C8-4F4F-955D-D8DCE44ED30C",
"node_data": {
"is_approved": false,
"is_node_complete": true,
"node_status": "complete"
}
},
{
"node_id": "BC7BD024-70F1-459B-BDBF-945A3EED666C",
"node_data": {
"is_approved": null,
"is_node_complete": false,
"node_status": "requested"
}
}
]
}
My query to update column
DECLARE #rec_id INT = 1;;
WITH personnel_CTE
AS (
SELECT *
FROM some_table AS acm
CROSS APPLY openjson(json_doc) WITH (personnel_node NVARCHAR(MAX) '$.personnel' AS json)
CROSS APPLY openjson(personnel_node) pn
WHERE id = #rec_id
AND cast(json_value(pn.value, '$.node_data.is_node_complete') AS BIT) = 0
)
UPDATE personnel_CTE
SET json_doc = json_modify(json_doc, '$.personnel[' + personnel_CTE.[key] + '].node_data.node_status', 'reviewer_assigned')
The guids are unique as well as the id for some_table. This is a truncated toy example, but these properties are the key items for the update.

I don't think that you can update the JSON content with this statement (updating one row with values from multiple rows), but you may try with the following approach, which parses the JSON data as a table using OPENJSON(), updates this table and outputs the table's content as JSON using FOR JSON PATH:
JSON:
DECLARE #json nvarchar(max) = N'
{
"personnel": [
{
"node_id": "FDA64E9F-3BAC-45FA-8819-8A086D96B359",
"node_data": {
"is_approved": null,
"is_node_complete": false,
"node_status": "requested"
}
},
{
"node_id": "AF829232-32F4-464B-8817-50ED24447AA4",
"node_data": {
"is_approved": null,
"is_node_complete": false,
"node_status": "requested"
}
},
{
"node_id": "E18F8197-B16D-4E0B-8EE9-DBF5B23A8EB5",
"node_data": {
"is_approved": true,
"is_node_complete": true,
"node_status": "complete"
}
},
{
"node_id": "286700AE-81C8-4F4F-955D-D8DCE44ED30C",
"node_data": {
"is_approved": false,
"is_node_complete": true,
"node_status": "complete"
}
},
{
"node_id": "BC7BD024-70F1-459B-BDBF-945A3EED666C",
"node_data": {
"is_approved": null,
"is_node_complete": false,
"node_status": "requested"
}
}
]
}'
Table and statement:
CREATE TABLE some_table (id int, json_doc nvarchar(max))
INSERT INTO some_table (id, json_doc) VALUES (1, #json)
INSERT INTO some_table (id, json_doc) VALUES (2, #json)
DECLARE #rec_id INT = 1;
UPDATE some_table
SET json_doc = (
SELECT
node_id AS 'node_id',
is_approved AS 'node_data.is_approved',
is_node_complete AS 'node_data.is_node_complete',
CASE
WHEN CONVERT(bit, is_node_complete) = 0 THEN 'reviewer_assigned'
ELSE node_status
END AS 'node_data.node_status'
FROM OPENJSON (json_doc, '$.personnel') WITH (
node_id nvarchar(36) '$.node_id',
is_approved bit '$.node_data.is_approved',
is_node_complete bit '$.node_data.is_node_complete',
node_status nvarchar(50) '$.node_data.node_status'
)
FOR JSON PATH, ROOT ('personnel'), INCLUDE_NULL_VALUES
)
WHERE id = #rec_id

Related

How to join nested JSON indices to multiple rows in SQL by primary key

I am trying to update several rows in SQL with JSON.
I'd like to match a primary key on a table row to an index nested in an array of JS objects.
Sample data:
let json = [{
"header": object_data,
"items": [{
"id": {
"i": 0,
"name": "item_id"
},
"meta": {
"data": object_data,
"text": "some_text"
}
}, {
"id": {
"i": 4,
"name": "item_id4"
},
"meta": {
"data": object_data,
"text": "some_text"
}
}, {
"id": {
"i": 17,
"name": "item_id17"
},
"meta": {
"data": object_data,
"text": "some_text"
}}]
}]
Sample table:
i | json | item_id
---+---------------------------+---------
0 | entire_object_at_index_0 | item_id
4 | entire_object_at_index_4 | item_id4
17 | entire_object_at_index_17 | item_id17
entire_object_at_index, meaning appending the item data to the header to create a new object for each row.
"header" "some_data",
"items": [{
"id": {
"i": 0,
"name": "item_id1"
},
"meta": {
"data": "some_data",
"text": "some_text"
}
}]
SQL:
update someTable set
json = json_value(#jsons, '$') -- not sure how to join on index here
item_id = json_value(#jsons, '$.items[?].id.name' -- not sure how to select by index here
where [i] = json_query(#jsons, '$.items.id.i')
The requirement to repeat the other properties complicates this a bit, because we need to build a new object explicitly. Even so it's not too hard:
update someTable
set
[json] = (
select (
select
"header" = json_query(#json, '$.header'),
"items" = json_query(N'[' + items.item + N']')
for json path, without_array_wrapper
)
),
item_id = items.item_id
from openjson(#json, '$.items') with (
item nvarchar(max) '$' as json,
item_id varchar(50) '$.id.name',
i int '$.id.i'
) items
join someTable on [someTable].i = items.i
Here I'm assuming the #json has already been unwrapped from its array, as your query seems to assume. If it's not, substitute $.[0] for $ in the outer query.
Update:
It's an attempt to improve my answer (I missed the header part of the JSON content in the original answer). Of course, the #JeroenMostert's answer is an excellent solution, so this is just another possible approach. Note, that if header part of JSON content is scalar value, you should use JSON_VALUE().
Table and JSON:
-- Table
CREATE TABLE #Data (
i int,
[json] nvarchar(max),
item_id nvarchar(100)
)
INSERT INTO #Data
(i, [json], [item_id])
VALUES
(0 , N'entire_object_at_index_0', N'item_id'),
(4 , N'entire_object_at_index_4', N'item_id4'),
(17, N'entire_object_at_index_17', N'item_id17')
-- JSON
DECLARE #json nvarchar(max) = N'[{
"header": {"key": "some_data"},
"items": [{
"id": {
"i": 0,
"name": "item_id"
},
"meta": {
"data": "some_data",
"text": "some_text"
}
}, {
"id": {
"i": 4,
"name": "item_id4"
},
"meta": {
"data": "some_data",
"text": "some_text"
}
}, {
"id": {
"i": 17,
"name": "item_id17"
},
"meta": {
"data": "some_data",
"text": "some_text"
}}]
}]'
Statement:
UPDATE #Data
SET #Data.Json = j.Json
FROM #Data
CROSS APPLY (
SELECT
JSON_QUERY(#json, '$[0].header') AS header,
JSON_QUERY(j.[value], '$') AS items
FROM OPENJSON(#json, '$[0].items') j
WHERE JSON_VALUE(j.[value], '$.id.i') = #Data.[i]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) j ([Json])
Original answer:
One possible approach is to use OPENJSON and appropriate join:
Table and JSON:
-- Table
CREATE TABLE #Data (
i int,
[json] nvarchar(max),
item_id nvarchar(100)
)
INSERT INTO #Data
(i, [json], [item_id])
VALUES
(0 , N'entire_object_at_index_0', N'item_id'),
(4 , N'entire_object_at_index_4', N'item_id4'),
(17, N'entire_object_at_index_17', N'item_id17')
-- JSON
DECLARE #json nvarchar(max) = N'[{
"header": "some_data",
"items": [{
"id": {
"i": 0,
"name": "item_id"
},
"meta": {
"data": "some_data",
"text": "some_text"
}
}, {
"id": {
"i": 4,
"name": "item_id4"
},
"meta": {
"data": "some_data",
"text": "some_text"
}
}, {
"id": {
"i": 17,
"name": "item_id17"
},
"meta": {
"data": "some_data",
"text": "some_text"
}}]
}]'
Statement:
UPDATE #Data
SET [json] = j.[value]
FROM #Data
LEFT JOIN (
SELECT
[value],
JSON_VALUE([value], '$.id.i') AS [i]
FROM OPENJSON(#json, '$[0].items')
) j ON (#Data.[i] = j.[i])

Convert table result to a form of JSON

I am trying to form json from a table result set :
create table testmalc(
appid int identity(1,1),
propertyid1 int ,
propertyid1val varchar(10) ,
propertyid2 int,
propertyid2val varchar(10) ,
)
insert into testmalc values(456,'t1',789,'t2')
insert into testmalc values(900,'t3',902,'t4')
need below desired JSON result :
{
"data": {
"record": [{
"id": appid,
"customFields": [{
"customfieldid": propertyid1 ,
"customfieldvalue": propertyid1val
},
{
"customfieldid": propertyid2 ,
"customfieldvalue": propertyid2val
}
]
},
{
"id": appid,
"customFields": [{
"customfieldid": propertyid1 ,
"customfieldvalue": propertyid1val
},
{
"customfieldid": propertyid2 ,
"customfieldvalue": propertyid2val
}
]
}
]
}
}
I am trying to use stuff but was not getting the desired result. Now trying with UnPivot.
If you cannot upgrade to SQL-Server 2016 for JSON support you should try to solve this in any application / programming language you know of.
Just for fun, I provide an approach, which works, but is more a hack than a solution:
Your test data:
DECLARE #testmalc table (
appid int identity(1,1),
propertyid1 int ,
propertyid1val varchar(10) ,
propertyid2 int,
propertyid2val varchar(10)
);
insert into #testmalc values(456,'t1',789,'t2')
,(900,'t3',902,'t4');
--create a XML, which is the most similar structure and read it as a NVARCHAR string
DECLARE #intermediateXML NVARCHAR(MAX)=
(
SELECT t.appid AS id
,(
SELECT t2.propertyid1 AS [prop1/#customfieldid]
,t2.propertyid1val AS [prop1/#customfieldvalue]
,t2.propertyid2 AS [prop2/#customfieldid]
,t2.propertyid2val AS [prop2/#customfieldvalue]
FROM #testmalc t2
WHERE t2.appid=t.appid
FOR XML PATH('customFields'),TYPE
) AS [*]
FROM #testmalc t
GROUP BY t.appid
FOR XML PATH('row')
);
--Now a bunch of replacements
SET #intermediateXML=REPLACE(REPLACE(REPLACE(REPLACE(#intermediateXML,'=',':'),'/>','}'),'<prop1 ','{'),'<prop2 ','{');
SET #intermediateXML=REPLACE(REPLACE(REPLACE(REPLACE(#intermediateXML,'<customFields>','"customFields":['),'</customFields>',']'),'customfieldid','"customfieldid"'),'customfieldvalue',',"customfieldvalue"');
SET #intermediateXML=REPLACE(REPLACE(#intermediateXML,'<id>','"id":'),'</id>',',');
SET #intermediateXML=REPLACE(REPLACE(REPLACE(#intermediateXML,'<row>','{'),'</row>','}'),'}{','},{');
DECLARE #json NVARCHAR(MAX)=N'{"data":{"record":[' + #intermediateXML + ']}}';
PRINT #json;
The result (formatted)
{
"data": {
"record": [
{
"id": 1,
"customFields": [
{
"customfieldid": "456",
"customfieldvalue": "t1"
},
{
"customfieldid": "789",
"customfieldvalue": "t2"
}
]
},
{
"id": 2,
"customFields": [
{
"customfieldid": "900",
"customfieldvalue": "t3"
},
{
"customfieldid": "902",
"customfieldvalue": "t4"
}
]
}
]
}
}

reading nested json data from Flex table in vertica

I have this json file.
[
{
"Modified": "2016-09-0",
"Id": 16,
"Name": "ABC",
"Filters": [],
"ScoreComponents":[
{
"Id": 86,
"Name": "Politeness",
"Bins": [],
"Ranges": [
{
"ComponentId": 86,
"LastUser": "CDE\\John.Doe"
},
{
"ComponentId": 86,
"LastUser": "CDE\\John.Doe"
}
],
"Filters": []
},
{
"Id": 87,
"Name": "Empathy",
"Bins": [],
"Ranges": [
{
"ComponentId": 87,
"LastUser": "CDE\\John.Doe"
}
],
"Filters": [
{
"ComponentID": -30356,
"BucketID": 81
}
]
},
{
"Id": 88,
"Name": "Ownership",
"Bins": [],
"Ranges": [
{
"ComponentId": 88,
"User": "CDE\\John.Doe"
}
],
"Filters": []
}]
}
]
I have loaded this file In Vertica flex table
CREATE FLEX TABLE flex_test();
copy events_stg.flex_test from LOCAL 'C:/test2.json' PARSER fjsonparser (flatten_maps= true, flatten_arrays = false)
I want to read all data from ScoreComponents including nested arrays.
I tried query this query
select "Id" as scoreid,mapitems("ScoreComponents") OVER(PARTITION BY
"Id") from flex_test
getting output like:
I just dont understand those small squares in output. I am a student and this vertica DB and Flex tables are new me.
I have tried with flatten_arrays = true but its giving me empty result set.
You're getting squares because the values field contains a binary VMap.
This should do it:
create flex table so_flex();
create table so_score_components(
id int,
name varchar(100)
);
create table so_ranges(
parent_id int,
component_id int,
last_user varchar(100)
);
create table so_filters(
parent_id int,
component_id int,
bucket_id int
);
copy so_flex from local 'E:\Demos\so.json'
parser fjsonparser(start_point='ScoreComponents',
flatten_maps = false, flatten_arrays = false);
insert into so_score_components(id, name)
select id::int, name::varchar from so_flex;
insert into so_ranges(parent_id, component_id, last_user)
select id::int, values['ComponentId']::int, values['LastUser']::varchar
from (
select id, mapitems(ranges) over (partition by id)
from so_flex
) t;
insert into so_filters(parent_id, component_id, bucket_id)
select id::int, values['ComponentID']::int, values['BucketID']::int
from (
select id, mapitems(filters) over (partition by id)
from so_flex
) t;

Postgres + Sequelize: How to read function result?

I have a function payment_summary() as below:
CREATE OR REPLACE FUNCTION payment_summary()
RETURNS SETOF PAYMENT_SUMMARY_TYPE
LANGUAGE plpgsql
AS $$
DECLARE
payment_sum payment_summary_type%ROWTYPE;
BEGIN
FOR payment_sum IN SELECT
pay.application_no,
project.title,
pay.payment_rec,
customer.cust_name,
project.estimated_cost,
(project.estimated_cost - pay.payment_rec) AS outstanding_amt
FROM project
INNER JOIN customer
ON project.customer_cust_id = customer.cust_id
INNER JOIN
(SELECT
project.application_no,
sum(payment.amount) AS payment_rec
FROM payment
INNER JOIN project
ON payment.project_id = project.project_id
WHERE payment.drcr_flg = 'Cr'
GROUP BY project.application_no) AS pay
ON pay.application_no = project.application_no
LOOP
RETURN NEXT payment_sum;
END LOOP;
END;
$$;
PAYMENT_SUMMARY_TYPE is defined as:
CREATE TYPE PAYMENT_SUMMARY_TYPE AS
(
application_no VARCHAR(150),
title VARCHAR(500),
payment_rec INTEGER,
customer_name VARCHAR(500),
estimated_cost INTEGER,
outstanding_amt INTEGER
);
Using below code to execute the function and get results:
sequelize.query('SELECT payment_summary()').then(function(data) {
res.json(data);
});
Getting below as response:
[
[
{
"payment_summary": "(716,\"C1\",100000,\"C1 - city\",0,-100000)"
},
{
"payment_summary": "(716,\"C2\",100000,\"C2 - city\",0,-100000)"
}
],
{
"command": "SELECT",
"rowCount": 2,
"oid": null,
"rows": [
{
"payment_summary": "(716,\"C1\",100000,\"C1 - city\",0,-100000)"
},
{
"payment_summary": "(716,\"C2\",100000,\"C2 - city\",0,-100000)"
}
],
"fields": [
{
"name": "payment_summary",
"tableID": 0,
"columnID": 0,
"dataTypeID": 17453,
"dataTypeSize": -1,
"dataTypeModifier": -1,
"format": "text"
}
],
"_parsers": [
null
],
"rowAsArray": false
}
]
I need the response in below format:
[
{
application_no: 716,
title: "C1",
payment_rec : 100000,
customer_name : "C1 - city"
estimated_cost : 0
outstanding_amt : -100000
},
{
application_no: 717,
title: "C2",
payment_rec : 100000,
customer_name : "C2 - city"
estimated_cost : 0
outstanding_amt : -100000
}
]
How can i read / convert the response in required format ?

Create json with column values as object keys

I have a table defined like this:
CREATE TABLE data_table AS (
id bigserial,
"name" text NOT NULL,
"value" text NOT NULL,
CONSTRAINT data_table_pk PRIMARY KEY (id)
);
INSERT INTO data_table ("name", "value") VALUES
('key_1', 'value_1'),
('key_2', 'value_2');
I would like to get a JSON object from this table content, which will look like this:
{
"key_1":"value_1",
"key_2":"value_2"
}
Now I'm using the client application to parse the result set into JSON format. Is it possible to accomplish this by a postgresl query?
If you're on 9.4 you can do the following:
$ select json_object_agg("name", "value") from data_table;
json_object_agg
----------------------------------------------
{ "key_1" : "value_1", "key_2" : "value_2" }
select
format(
'{%s}',
string_agg(format(
'%s:%s',
to_json("name"),
to_json("value")
), ',')
)::json as json_object
from data_table;
json_object
---------------------------------------
{"key_1":"value_1","key_2":"value_2"}
In a generic scenario you can nest more than one json_object_agg functions on top of a subquery. The inner subqueries should always have at least one column that will be used by outer subquery as keys for the json_object_agg function.
In the example, in the subquery C the values of the column action are used as keys in the subquery A. In A the values of column role are used as keys in query A.
-- query A
select json_object_agg(q1.role, q1.actions) from (
-- subquery B
select q2.role, json_object_agg(q2.action, q2.permissions) as actions from (
-- subquery C
select r.name as role, a.name as action, json_build_object (
'enabled', coalesce(a.bit & bit_and(p.actionids) <> 0, false),
'guestUnsupported', r.name = 'guest' and a."guestUnsupported"
) as permissions
from role r
left join action a on a.entity = 'route'
left join permission p on p.roleid = r.id
and a.entity = p.entityname
and (p.entityid = 1 or p.entityid is null)
where
1 = 1
and r.enabled
and r.deleted is null
group by r.name, a.id
) as q2 group by q2.role
) as q1
The result is a single row/single column with the following content:
{
"Role 1": {
"APIPUT": {
"enabled": false,
"guestUnsupported": false
},
"APIDELETE": {
"enabled": false,
"guestUnsupported": false
},
"APIGET": {
"enabled": true,
"guestUnsupported": false
},
"APIPOST": {
"enabled": true,
"guestUnsupported": false
}
},
"Role 2": {
"APIPUT": {
"enabled": false,
"guestUnsupported": false
},
"APIDELETE": {
"enabled": false,
"guestUnsupported": false
},
"APIGET": {
"enabled": true,
"guestUnsupported": false
},
"APIPOST": {
"enabled": false,
"guestUnsupported": false
}
}
}