SQL to JSON with multiple nested arrays at same level - json

I'm using SQL Server 2016 to create a JSON file with multiple nested elements. The final JSON needs to have two nested arrays on the same level, as in array1a and array1b in the simplified example below, with array1 itself being a nested array in the final document.
{"array1": [
{
"array1a": [
{
"string": "text",
"number": 25.7
}
],
"array1b": [
{
"object": {
"type": "foo",
"value": "bar"
}
}
]
}
]
}
I tried using subqueries with FOR JSON PATH, but which formats array1a and array1b correctly, but array1 always comes back as an object, not as an array.
[array1.array1a] = (SELECT
StringField AS string
, NumberField AS number
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
, [array1.array1b] = (SELECT
ObjectType AS [object.type]
, ObjectValue AS [object.value]
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
That gives me the JSON below, without the square brackets that wrap the contents of array1.
{"array1": {
"array1a": [
{
"string": "text",
"number": 25.7
}
],
"array1b": [
{
"object": {
"type": "foo",
"value": "bar"
}
}
]
}
}
I also tried concatenating the two sub-queries together as in the example below, but the JSON output is escaped.
[array1] = CONCAT(
(SELECT
StringField AS [array1a.string]
, NumberField AS [array1a.number]
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
, (SELECT
ObjectType AS [array1b.object.type]
, ObjectValue AS [array1b.object.value]
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
)
Result:
"array1": "[{\"array1a.string\":\"text\",\"array1a.number\":25.7}][{\"array1b\":{\"object\":{\"type\":\"foo\",\"value\":\"bar\"}}}]"
Any thoughts on how I can get two nested arrays at the same hierarchy level that are themselves part of a nested array?

Related

Is it possible to add a key to a value that does not have a key in JSON?

I have a JSON without a key but just a value. Is it possible to add a key to it using React?
Here I have the and hanging without a key. How do I add a key to it, so that it's easier to read? I looked at JSON.Stringify and checked the replacer but it does not let me do it. delete also does not let me do it.
{
"sample": [
{
"id": "r-1",
"name": "sam"
},
"and",
{
"id": "r-2",
"name": "jerry"
}
]
}
You're confusing objects and arrays. The "key" with the string and as a value isn't a key. It's an array element (the value of the sample property is an array). It can't have a key unless you convert it from a string to an object, like the array elements before and after it, which are objects.
The structure of the array on the sample object property looks like this:
object | string | object
If you're ok changing your data from a string to an object, just replace the second array element (index 1) in the sample property with a new object:
const myObj = {
"sample": [{
"id": "r-1",
"name": "sam"
},
"and",
{
"id": "r-2",
"name": "jerry"
}
]
}
myObj.sample[1] = {
newKey: myObj.sample[1]
};
console.log(myObj);

Couchbase N1QL - Nest array using keys

i'm new to Couchbase and N1QL syntax and i'm facing an issue.
Let's say we have 3 type of documents:
Doc1 of TypeA with key = typeA:Doc1
{
"type": "typeA"
"id": "Doc1",
"sequences": [
"typeB:Doc2"
]
}
Doc2 of TypeB with key = typeB:Doc2
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "typeC:Doc3"
}
]
}
Doc3 of TypeC with key = typeC:Doc3
{
"id": "Doc3",
"prop": "value"
}
What i want to achieve is to nest these 3 objects by their document keys in ordere to have a unique document with this structure:
{
"id": "Doc1",
"sequences": [
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "Doc3",
"prop": "value"
}
]
}
]
What i've done is to nest the first two documents to obtain a partial result. But i'm tryng to integrate also the third document.
Here's my attempt:
SELECT dev.*,
ARRAY sq_i FOR sq_i IN prseq END AS sequences
FROM data dev
NEST data prseq ON KEYS dev.sequences
WHERE dev.type = 'TypeA'
Can anyone help me with the third level of nesting?
Thank you.
Use subqueries
SELECT dt.*,
(SELECT ds.*,
(ARRAY OBJECT_ADD((SELECT RAW dp FROM data AS dp USE KEYS v.id)[0], "order", v.`order`)
FOR v IN ds.processors
END) AS processors
FROM data AS ds USE KEYS dt.sequences) AS sequences
FROM data AS dt
WHERE dt.type = 'TypeA';

Can't parsing an array of objects with Dynamic Keys using OPENJSON And SQL Server 2017

I'm after running into some trouble parsing a set of JSON documents using SQL Server 2017.
I've encountered a nested array (sample below) within a document which uses dynamic ObjectId's i.e. 123 as the object Key as opposed to using a static key i.e. Category and then having a separate key:value to ref the ObjectId.
As a result I can't extract the items to a table using the regular CROSS APPLY OPENJSON syntax without specifying each individual Object Id (there are thousands)?
Is there a way to do this without referencing each ObjectId explicitly ideally i'd like to return all product items in a table and just have a field withe the categoryId.
"ProductItems": {
"123": [
{
"item": "13663"
}
]
"124": [
{
"value": "2336"
},
{
"value": "3667"
}
],
"453": [
{
"value": "8667"
},
{
"value": "1956"
}
]
}
Try something like this:
DECLARE #x NVARCHAR(MAX)=
'{"ProductItems":{
"123": [
{
"item": "13663"
}
],
"124": [
{
"value": "2336"
},
{
"value": "3667"
}
],
"453": [
{
"value": "8667"
},
{
"value": "1956"
}
]
}}'
SELECT j2.*, j3.* FROM OPENJSON(#x) j1
CROSS APPLY OPENJSON(j1.Value) j2
CROSS APPLY OPENJSON(j2.Value) j3

select values in json into other json (Postgres)

I have the following JSON
{
"eventSummaryList": [
{
"customer": "189256",
"data": "{\"cliente\":\"189256\",\"data_posicao\":\"1491426372\",\"gps_valido\":\"1\",\"horimetro\":\"120561\",\"ibuttonHex\":\"0\",\"ibuttonPart1\":\"0\",\"ibuttonPart2\":\"0\",\"id_evento\":\"null\",\"id_motorista\":\"0\",\"ignicao\":\"1\",\"latitude\":\"-2222222\",\"longitude\":\"-2222222\",\"odometro\":\"253692\",\"pos_memoria\":\"0\",\"veiculo\":\"44444\",\"velocidade\":\"50\"}",
"identifierEventRule": "77404",
"identifierRule": "6",
"identifierSummary": "28901976",
"rule": "velocidade_maior_que",
"status": 1,
"vehicle": "44444"
}
],
"header": {
"mensagem": {
"estilo": "SUCCESS",
"mensagem": "Successfully executed service",
"plataforma": "EVENT_POINT",
"status": "SUCESSO"
}
}
}
And I need to extract the value "velocidade" what's inside "data" that
contains another json.
I'm using the following syntax but it returns null.
select cast((value::json ->'data')::json->> 'velocidade' AS int) AS velocidade,
Try:
SELECT ((value::json #>> '{eventSummaryList,0,data}')::json ->> 'velocidade')::int
(The #>> or ->> operators are the key. If you use #> or ->, you'll end up with a json-encoded json string. BTW this is really a messed up model, I would look into fixing its input first/instead.)
http://rextester.com/THVYFK9026

Nested JSON query in postgres

I have some JSON along the following lines, the format of which cannot, unfortunately, be changed:
{
"elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
This is stored in a postgres database and I'd like to pull out records by means of the id embedded in the JSON above. So far I've tried stuff like this:
SELECT "data".* FROM "data" WHERE payload #>> '{elements,nodes,data,id}' = 'n0';
...without success; although this query runs it returns nothing. Can anyone suggest how this might be done?
Create schema:
create table json (
id serial primary key,
data jsonb
);
insert into json (data) values (
'{ "elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
'::jsonb);
Query itself:
select * from json js,jsonb_array_elements(data->'elements'->'nodes') as node
where node->'data'->>'id' = 'n0';