Currently you cannot directly consume json data that starts with an array.
It is not supported by JSON_TABLE
What you can do, is to embedd your array in an object and parse the result.
Something like this:
Select *
from JSON_TABLE('{ "root": ' concat
systools.HTTPGETCLOB('
https://pkgstore.datahub.io/core/country-list/latest/d
ata/json/data.json' , '')
concat '}',
'$.root[*]'
Columns( "Code" VarChar(5),
"Name" VarChar(50))) x;
This web service returns:
[{"Code": "AF", "Name": "Afghanistan"},{"Code": "AX", "Name": "\u00c5land
Islands"}, ....]
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them
and keeping them!"
?Train people well enough so they can leave, treat them well enough so they
don't want to.? (Richard Branson)
-----Original Message-----
From: RPG400-L <rpg400-l-bounces@xxxxxxxxxxxx> On Behalf Of Mendoza, Lynelle
I. M.
Sent: Donnerstag, 2. August 2018 14:01
To: rpg400-l@xxxxxxxxxxxx
Subject: Using JSON_TABLE on an array that starts with a square bracket
Hello All,
I was able to get the name and country of JSON array below if it starts with
a curly bracket:
{"items":[{"name": "MyFirstCompany","country": "DE"},{"id": 2,"name":
"MySecondCompany", "country": "DE"}]}
using this SQL command:
with y as (
Select * from JSON_TABLE(get_clob_from_file('/test.json'), '$'
columns( nested '$.items[*]' columns(
"name" varchar(30),
"country" varchar(02)
) ) ) x ) select * from y with ur;
However, the JSON data I want to parse starts with a square bracket and
looks like this:
'[{"name": "MyFirstCompany","country": "DE"},{"id": 2,"name":
"MySecondCompany", "country": "DE"}]'
But I wasn't able to get any rows using this command:
with y as (
Select * from JSON_TABLE(
'[{"name": "MyFirstCompany","country": "DE"},{"id": 2,"name":
"MySecondCompany", "country": "DE"}]', '$'
columns(
nested 'lax $[*]' columns(
"name" varchar(30),
"country" varchar(02)
) ) ) x
) select * from y with ur;
Kindly advise on correct command on parsing JSON array that starts with a
square bracket.
Thanks,
Lynelle
________________________________
This message is for the designated recipient only and may contain
privileged, proprietary, or otherwise confidential information. If you have
received it in error, please notify the sender immediately and delete the
original. Any other use of the e-mail by you is prohibited. Where allowed by
local law, electronic communications with Accenture and its affiliates,
including e-mail and instant messaging (including content), may be scanned
by our systems for the purposes of information security and assessment of
internal compliance with Accenture policy. Your privacy is important to us.
Accenture uses your personal data only in compliance with data protection
laws. For further information on how Accenture processes your personal data,
please see our privacy statement at
https://www.accenture.com/us-en/privacy-policy.
____________________________________________________________________________
__________
www.accenture.com
--
This is the RPG programming on the IBM i (AS/400 and iSeries) (RPG400-L)
mailing list To post a message email: RPG400-L@xxxxxxxxxxxx To subscribe,
unsubscribe, or change list options,
visit:
https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at
https://archive.midrange.com/rpg400-l.
Please contact support@xxxxxxxxxxxx for any subscription related questions.
Help support midrange.com by shopping at amazon.com with our affiliate link:
http://amzn.to/2dEadiD
As an Amazon Associate we earn from qualifying purchases.