Parse Json using Oracle SQL - sql

I am trying to parse JSON in Oracle SQL.
Oracle DB version 12.1.0.2
{
"Rownum": "1",
"Name": "John",
"AddressArray":["Address1", "Address2"],
"TextObj":[{
"mName" : "Carol",
"lName" : "Cena"
},
{
"mName" : "Mark",
"lName" : "Karlo"
}
]
}
output should look like below:

I suppose "nested" will do the trick
select * from json_Table('{"Rownum": "1", "Name": "John", "AddressArray":["Address1", "Address2"], "TextObj":[{"mName" : "Carol","lName" : "Cena",}]}', '$' columns (rownr number path '$.Rownum',
name varchar2(100) path '$.Name',
mName varchar2(100) path '$.TextObj[*].mName',
lName varchar2(100) path '$.TextObj[*].lName',
nested path '$.AddressArray[*]' columns(AddressArray varchar2(100) path '$')
));
My output:
ROWNR
NAME
MNAME
LNAME
ADDRESSARRAY
1
John
Carol
Cena
Address1
1
John
Carol
Cena
Address2

Related

Use custom string value in T-SQL OPENJSON With clause

DECLARE #json NVARCHAR(4000) = N'{
"pets" : {
"cats" : [
{ "id" : 1, "name" : "Fluffy", "sex" : "Female" },
{ "id" : 2, "name" : "Long Tail", "sex" : "Female" },
{ "id" : 3, "name" : "Scratch", "sex" : "Male" }
]
}
}'
SELECT * FROM OPENJSON(#json, '$.pets.cats') WITH --we have the "main" json address here
(
id INT '$.id', --sub-address
name varchar(10) '$.name', --sub-address
sex varchar(10) '$.sex' --sub-address
)
The results are:
id
name
sex
1
Fluffy
Female
2
Long Tail
Female
3
Scratch
Male
I want to include another column which will not depend on the JSON, but be a custom specified string ("mammal"), so that the output is like this:
id
name
sex
Type
1
Fluffy
Female
mammal
2
Long Tail
Female
mammal
3
Scratch
Male
mammal
How would I achieve this?
You can simply add a constant in your SELECT statement:
DECLARE #json NVARCHAR(4000) = N'{
"pets" : {
"cats" : [
{ "id" : 1, "name" : "Fluffy", "sex" : "Female" },
{ "id" : 2, "name" : "Long Tail", "sex" : "Female" },
{ "id" : 3, "name" : "Scratch", "sex" : "Male" }
]
}
}'
SELECT *, 'mammal' AS Type FROM OPENJSON(#json, '$.pets.cats') WITH --we have the "main" json address here
(
id INT '$.id', --sub-address
name varchar(10) '$.name', --sub-address
sex varchar(10) '$.sex' --sub-address
)

Nested JSON using ms sql server

I have a table with USER_DATA (user data table) with 2 rows (2 entries basically)
I created one nested JSON query ->
SELECT CONCAT( first_name,' ', last_name) AS displayName,
first_name AS givenName, last_name AS surname,
identities = (SELECT login_name AS issuerAssignedId
FROM user_data
FOR JSON AUTO)
FROM user_data
FOR JSON PATH, ROOT('users');
Here, I am getting this output ->
{
"users": [
{
"displayName": "David Dave",
"givenName": "David",
"surname": "Dave",
"identities": [
{
"issuerAssignedId": "System"
},
{
"issuerAssignedId": "Administrators"
}
]
},
{
"displayName": "Tony Padila",
"givenName": "Tony",
"surname": "Padila",
"identities": [
{
"issuerAssignedId": "System"
},
{
"issuerAssignedId": "Administrators"
}
]
}
But the problem is -> inside identities,
"issuerAssignedId": "System" ----> Belongs to Dave
"issuerAssignedId": "Administrators" ----> Belongs to Tony
But I am not able to stop the inner select query (Not able to map correctly)
The correct output should be --->
{
"users": [
{
"displayName": "David Dave",
"givenName": "David",
"surname": "Dave",
"identities": [
{
"issuerAssignedId": "System"
}
]
},
{
"displayName": "Tony Padila",
"givenName": "Tony",
"surname": "Padila",
"identities": [
{
"issuerAssignedId": "Administrators"
}
]
}
PLEASE HELP.
You are missing the condition in the inner query and why do you want the identities to be a separate array in the JSON output.
I have updated the query as per my understanding please refer below sql, I'm not sure why you are having the ** in the query
SELECT CONCAT (
FIRST_NAME
,' '
,LAST_NAME
) AS displayName
,FIRST_NAME AS givenName
,LAST_NAME AS surname
,identities = (
SELECT innr.LOGIN_NAME AS issuerAssignedId
FROM USER_DATA
innr
WHERE
innr.LOGIN_NAME = ottr.LOGIN_NAME
FOR JSON AUTO
)
FROM USER_DATA ottr
FOR JSON PATH
,ROOT('users');

Oracle - Parsing Multiple JSON Values in a single SQL query

I have data in PersonalDetails table in JSON format, I am trying to parse the data in a single query, currently I am using JSON_TABLE functions, in my query I am only able to parse only one column, if I try to use JSON_TABLE functions twice it throwing me an error, is their a way to parse all the columns in a single query? or is their any other function apart from JSON_TABLE?
Sample Data
{
"FirstName" : "John",
"LastName" : "Doe",
"Job" : "Clerk",
"Address" : {
"Street" : "99 My Street",
"City" : "My City",
"Country" : "UK",
"Postcode" : "A12 34B"
},
"ContactDetails" : {
"Email" : "john.doe#example.com",
"Phone" : "44 123 123456",
"Twitter" : "#johndoe"
},
"DateOfBirth" : "01-JAN-1980",
"Active" : true
}
My Query
SELECT
FirstName,
LastName,
Job,
Street,
City,
Country,
Postcode,
ContactDetails,
DateOfBirth,
Active
FROM
JSON_TABLE(tab.Address, '$' COLUMNS
( Address VARCHAR(255) PATH '$.Street',
City VARCHAR(255) PATH '$.City',
Country VARCHAR(255) PATH '$.Country',
Postcode VARCHAR(255) PATH '$.Postcode',
)) JT,
PersonalDetails tab;
with PersonalDetails (jsn) as (
select '{
"FirstName" : "John",
"LastName" : "Doe",
"Job" : "Clerk",
"Address" : {
"Street" : "99 My Street",
"City" : "My City",
"Country" : "UK",
"Postcode" : "A12 34B"
},
"ContactDetails" : {
"Email" : "john.doe#example.com",
"Phone" : "44 123 123456",
"Twitter" : "#johndoe"
},
"DateOfBirth" : "01-JAN-1980",
"Active" : true
}' from dual
)
select jt.* from PersonalDetails, json_table (
PersonalDetails.jsn, '$'
COLUMNS
Firstname VARCHAR2(30) PATH '$.FirstName',
Address VARCHAR2(255) PATH '$.Address.Street',
City VARCHAR2(255) PATH '$.Address.City',
Country VARCHAR2(255) PATH '$.Address.Country',
Postcode VARCHAR2(255) PATH '$.Address.Postcode',
dob VARCHAR2(11) PATH '$.DateOfBirth',
email VARCHAR2(50) PATH '$.ContactDetails.Email'
) jt;
You could use the NESTED clause:
with PersonalDetails (data) as (
select
'{
"FirstName" : "John",
"LastName" : "Doe",
"Job" : "Clerk",
"Address" : {
"Street" : "99 My Street",
"City" : "My City",
"Country" : "UK",
"Postcode" : "A12 34B"
},
"ContactDetails" : {
"Email" : "john.doe#example.com",
"Phone" : "44 123 123456",
"Twitter" : "#johndoe"
},
"DateOfBirth" : "01-JAN-1980",
"Active" : true
}'
from dual
)
SELECT jt.*
FROM PersonalDetails,
JSON_TABLE(data, '$'
COLUMNS (FirstName VARCHAR2(50), LastName VARCHAR2(50), Job, Active,
NESTED ContactDetails COLUMNS (Email VARCHAR2(100), Phone)
)
) jt;
Output:
FIRSTNAME LASTNAME JOB ACTIVE EMAIL PHONE
---------- -------- ----- ------ -------------------- ---------------
John Doe Clerk true john.doe#example.com 44 123 123456

Apache NiFi QueryRecord SELECT Static Alias Column

I want to import a file that has the following Avro schema assigned using Apache NiFi:
{
"type" : "record",
"namespace" : "SomeSpaceName",
"name" : "SampleFile",
"fields" : [
{ "name" : "PersonName" , "type" : "string" },
{ "name" : "PersonType" , "type" : "string" }
]
}
When I use the QueryRecord processor I want to have a static field in the output file so I can import it into MongoDB. The query is:
SELECT LOWER(PersonName) as _id,
'Male' as gender
FROM flowfile
The problem is Calcite will not add the new static field properly. It adds the name successfully but the new gender field only contains the first letter of the word:
| _id | gender |
|------|--------|
| Eric | M |
| Bill | M |
| Chad | M |
Make sure QueryRecord processor writer avro schema have _id,gender fields included in it.
Writer Avro Schema:
{
"type" : "record",
"namespace" : "SomeSpaceName",
"name" : "SampleFile",
"fields" : [
{ "name" : "_id" , "type" : ["null","string"] },
{ "name" : "gender" , "type" : ["null","string"] }
]
}

Inserting into sql server tables (2016) from json

I have some json like this:
SET #json =N'[
{ "id" : 2,"name": "John", "surname": "Smith", "lastname":"", "age": 25 },
]'
I need to insert into table with a condition like IF LASTNAME IS EMPTY IN JSON THEN INSERT SURNAME INTO LASTNAME ELSE INSERT LASTNAME INTO LASTNAME
insert into mytable (id,firstname,lastname,age)
select id,name,case statement,age from openjson
WITH (id int 'strict $.id',name nvarchar(100) '$.name',case statement, age int '$.age');
Hi i think this query can respond :
DECLARE #json varchar(MAX)
SET #json =N'[
{ "id" : 2,"name": "John", "surname": "Smith", "lastname":"", "age": 25 },
{ "id" : 2,"name": "John", "surname": "Smith", "lastname":"TT", "age": 25 },
{ "id" : 2,"name": "John", "surname": "Smith", "lastname":"TEST", "age": 25 }
]'
select id,name,case when lastname = '' then surname else lastname end as lastnameup, age from openjson(#json)
WITH (id int 'strict $.id',name nvarchar(100) '$.name', surname nvarchar(100) '$.surname', lastname nvarchar(100) '$.lastname', age int '$.age');
EDIT for surname propose thanks to #Shnugo