How to get odata.nextLink from the returned JSON object using oracle's apex_json - json

I am retrieving data from an odata service. The response contains a link for loading more data i.e. odata.nextLink which I need to retrieve to load more data. How do I do this using oracle's apex_json?
The server's response is something like this:
{
"odata.metadata":"http://localhost:60497/odata/$metadata#tables","value":[
{"id":001,"name":"abc" }
.
.
],
"odata.nextLink":"http://localhost:60497/odata/tables?$skip=10"
}
Normally I would parse the data and then retrieve the information like this next_link := apex_json.get_varchar2('odata.nextLink'); but since it contains a point that won't work.

In this case:
apex_json.get_varchar2('odata.nextLink');
it is considering nextLink as the value of odata.
DECLARE
v_json APEX_JSON.T_VALUES;
v_str_json VARCHAR2(4000);
next_link VARCHAR2(4000);
BEGIN
v_str_json := '{
"odata" : {"nextLink" : "mylink"},
"odata.metadata":"http://localhost:60497/odata/$metadata#tables","value":[
{"id":001,"name":"abc" }
],
"odata.nextLink":"http://localhost:60497/odata/tables?$skip=10"
}
}';
APEX_JSON.PARSE(v_json, v_str_json);
next_link := APEX_JSON.GET_VARCHAR2(p_path => 'odata.nextLink', p_values => v_json);
dbms_output.put_line(next_link);
END;
>> OUTPUT: mylink
I do not know how to say "odata.nextLink" to be interpreted literally; and not as a path.
If nobody knows:
1 - or stop sending "odata." at the beginning of the name of each attribute;
2 - or use replace to remove.

Related

Call remote server through oracle apex

How to call external server or remote server from Oracle APEX using various restful services kindly let me know.
Thanks & Regards,
Yokes G
One way to do it is to set up a Rest Data Source entry. I am using Oracle Apex 20.2.
eg called myRest
using Simple HTTP
Remote Server: Server name without https: include the endpoint
JSON Operation: GET
Parameters: the header parameters
It is very easy to copy and paste this from PostMan.
Then you can use the following type of code to access this REST service
declare
l_params apex_exec.t_parameters;
begin
apex_exec.add_parameter( l_params, 'content', 'hello david' );
apex_exec.add_parameter( l_params, 'to', '123456' );
apex_exec.execute_rest_source(
'myRest',
'GET',
NULL,
l_params );
end;
Note that I couldn't get the JSON to work properly using Rest Data Sources and so I use make_rest_request instead as follows:
declare
l_host constant varchar2(200) := 'https://platform.xxx.com';
l_host_path constant varchar2(200) := l_host || '/v1/message';
l_clob clob;
l_body clob;
begin
l_body := '{
"messages": [
{
"channel": "xxx",
"to": "123",
"content": "Hello from APEX"
}
]
}';
apex_web_service.g_request_headers(1).name := 'Content-Type';
apex_web_service.g_request_headers(1).value := 'application/json';
apex_web_service.g_request_headers(2).name := 'Authorization';
apex_web_service.g_request_headers(2).value := 'yourkey';
l_clob := apex_web_service.make_rest_request(
p_url => l_host_path,
p_http_method => 'POST',
p_body => l_body);
end;
This second method is nice as then everything is in one place and you don't need to reference the REST Data Sources.

Parse unknown JSON path in TSQL with openjson and/or json_value

I have a incoming data structure that looks like this:
declare #json nvarchar(max) = '{
"action": "edit",
"data": {
"2077-09-02": {
"Description": "some stuff",
"EffectDate": "2077-1-1"
}
}
}';
To give you a long story short, I think TSQL hates this json structure, because no matter what I have tried, I can't get to any values other than "action".
The {data} object contains another object, {2077-09-02}. "2077-09-02" will always be different. I can't rely on what that date will be.
This works:
select json_value(#json, '$.action');
None of this works when trying to get to the other values.
select json_value(#json, '$.data'); --returns null
select json_value(#json, '$.data[0]'); --returns null
select json_value(#json, 'lax $.data.[2077-09-02].Description');
--JSON path is not properly formatted. Unexpected character '[' is found at position 11.
select json_value(#json, 'lax $.data.2077-09-02.Description');
--JSON path is not properly formatted. Unexpected character '2' is found at position 11.
How do I get to the other values? Is the JSON not perfect enough for TSQL?
It is never a good idea to use the declarative part of a text based container as data. The "2077-09-02" is a valid json key, but hard to query.
You can try this:
declare #json nvarchar(max) = '{
"action": "edit",
"data": {
"2077-09-02": {
"Description": "some stuff",
"EffectDate": "2077-1-1"
}
}
}';
SELECT A.[action]
,B.[key] AS DateValue
,C.*
FROM OPENJSON(#json)
WITH([action] NVARCHAR(100)
,[data] NVARCHAR(MAX) AS JSON) A
CROSS APPLY OPENJSON(A.[data]) B
CROSS APPLY OPENJSON(B.[value])
WITH (Description NVARCHAR(100)
,EffectDate DATE) C;
The result
action DateValue Description EffectDate
edit 2077-09-02 some stuff 2077-01-01
The idea:
The first OPENJSON will return the action and the data.
I use a WITH clause to tell the engine, that action is a simple value, while data is nested JSON
The next OPENJSON dives into data
We can now use B.[key] to get the json key's value
Now we need another OPENJSON to dive into the columns within data.
However: If this JSON is under your control I'd suggest to change its structure.
Use double quotes instead of []. JSON Path uses JavaScript's conventions where a string is surrounded by double quotes. The documentation's example contains this path $."first name".
In this case :
select json_value(#json,'$.data."2077-09-02".Description');
Returns :
some stuff
As for the other calls, JSON_VALUE can only return scalar values, not objects. You need to use JSON_QUERY to extract JSON objects, eg :
select json_query(#json,'$.data."2077-09-02"');
Returns :
{
"Description": "some stuff",
"EffectDate": "2077-1-1"
}

Pass data from JSON to variable for comparison

I have a request that I make in an API using GET
LWP::UserAgent,
the data is returned as JSON, with up to two results at most as follows:
{
"status":1,
"time":1507891855,
"response":{
"prices":{
"nome1\u2122":{
"preco1":1111,
"preco2":1585,
"preco3":1099
},
"nome2":{
"preco1":519,
"preco2":731,
"preco3":491
}
}
}
}
Dump:
$VAR1 = {
'status' => 1,
'time' => 1507891855,
'response' => {
'prices' => {
'nome1' => {
'preco1' => 1111,
'preco3' => 1099,
'preco2' => 1585
},
'nome2' => {
'preco3' => 491,
'preco1' => 519,
'preco2' => 731
}
}
}
};
What I would like to do is:
Take this data and save it in a variable to make a comparison using if with another variable that already has the name stored. The comparison would be with name1 / name2 and if it is true with the other variable it would get preco2 and preco3 to print everything
My biggest problem in the case is that some of these names in JSON contain characters like (TradeMark) that comes as \u2122 (some cases are other characters), so I can not make the comparison with the name of the other variable that is already with the correct name
nome1™
If I could only save the JSON already "converted" the characters would help me with the rest.
Basically after doing the request for the API I want to save the contents in a variable already converting all \u2122 to their respective character (this is the part that I do not know how to do in Perl) and then using another variable to compare them names are equal to show the price
Thanks for the help and any questions please tell me that I try to explain again in another way.
If I understand correctly, you need to get the JSON that you receive in UTF8 format to an internal variable that you can process. For that, you may use JSON::XS:
use utf8;
use JSON::XS;
my $name = "nome1™";
my $var1 = decode_json $utf8_encoded_json_text;
# Compare with name in $name
if( defined $var1->{'response'}->{'prices'}->{$name} ) {
# Do something with the name that matches
my $match = $var1->{'response'}->{'prices'}->{$name};
print $match->{'preco1'}, "\n";
}
Make sure you tell the Perl interpreter that your source is in UTF8 by specifying use utf8; at the beginning of the script. Then make sure you are editing the script with an editor that supports that format.
The function decode_json will return a ref to the converted value. In this case a hash ref. From there you work your way into the JSON.
If you know $name is going to be in the JSON you may omit the defined part. Otherwise, the defined clause will tell you whether the hash value is there. One you know, you may do something with it. If the hash values are a single word with no special characters, you may use $var1->{response}->{prices}->{$name}, but it is always safer to use $var1->{'response'}->{'prices'}->{$name}. Perl gets a bit ugly handling hash refs...
By the way, in JSON::XS you will also find the encode_json function to do the opposite and also an object oriented interface.

Extract specific values from JSON array

I have a JSON response that is formatted this way:
- Client 1
- Date: 15.07.2017
- Name: John
- URL: www.google.com
- Client 2
- Date: 15.07.2017
- Name: Jane
- URL: www.google.com
- Client N...
How could I extract only the Name & URL value from each client so I could add them to a listbox for example? Also please note that "Client 1" could be named otherwise, like "User 1" or just "1", that's not important, but the code should extract the values regardless of the parent object name.
PS: Sorry for missleading, the JSON format above was pseudo-code from memory, the actual format is:
[
{
"date":"xxx",
"name":"xxx",
"url":"xxx"
},
{
"date":"xxx",
"name":"xxx",
"url":"xxx"
},
{
"date":"xxx",
"name":"xxx",
"url":"xxx"
}
]
Answer in case anybody is looking.
procedure Answer;
var
JSON: string;
ClientItem: TJSONValue;
ClientList: TJSONArray;
ListBoxItem: TListBoxItem;
begin
JSON := TFile.ReadAllText('.\your-file.json');
ClientList := TJSONObject.ParseJSONValue(JSON) as TJSONArray;
if Assigned(ClientList) then
try
ListBox.Items.BeginUpdate;
try
for ClientItem in ClientList do
begin
ListBoxItem := TListBoxItem.Create(ListBox);
ListBoxItem.StyleLookup := 'CustomListbox';
ListBoxItem.StylesData['URL'] := ClientItem.GetValue<string>('url');
ListBoxItem.StylesData['Name'] := ClientItem.GetValue<string>('name');
Listbox.AddObject(ListBoxItem);
end;
finally
Listbox.Items.EndUpdate;
end;
finally
ClientList.Free;
end;
end;

USql Call data in multidimensional JSON array

I have this JSON file in a data lake that looks like this:
{
"id":"398507",
"contenttype":"POST",
"posttype":"post",
"uri":"http://twitter.com/etc",
"title":null,
"profile":{
"#class":"PublisherV2_0",
"name":"Company",
"id":"2163171",
"profileIcon":"https://pbs.twimg.com/image",
"profileLocation":{
"#class":"DocumentLocation",
"locality":"Toronto",
"adminDistrict":"ON",
"countryRegion":"Canada",
"coordinates":{
"latitude":43.7217,
"longitude":-31.432},
"quadKey":"000000000000000"},
"displayName":"Name",
"externalId":"00000000000"},
"source":{
"name":"blogs",
"id":"18",
"param":"Twitter"},
"content":{
"text":"Description of post"},
"language":{
"name":"English",
"code":"en"},
"abstracttext":"More Text and links",
"score":{}
}
}
in order to call the data into my application, I have to turn the JSON into a string using this code:
DECLARE #input string = #"/MSEStream/{*}.json";
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
#allposts =
EXTRACT
jsonString string
FROM #input
USING Extractors.Text(delimiter:'\b', quoting:true);
#extractedrows = SELECT Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(jsonString) AS er FROM #allposts;
#result =
SELECT er["id"] AS postID,
er["contenttype"] AS contentType,
er["posttype"] AS postType,
er["uri"] AS uri,
er["title"] AS Title,
er["acquisitiondate"] AS acquisitionDate,
er["modificationdate"] AS modificationDate,
er["publicationdate"] AS publicationDate,
er["profile"] AS profile
FROM #extractedrows;
OUTPUT #result
TO "/ProcessedQueries/all_posts.csv"
USING Outputters.Csv();
This output the JSON into a .csv file that is readable and when I download the file all data is displayed properly. My problem is when I need to get the data inside profile. Because the JSON is now a string I can't seem to extract any of that data and put it into a variable to use. Is there any way to do this? or do I need to look into other options for reading the data?
You can use JsonTuple on the profile string to further extract the specific properties you want. An example of U-SQL code to process nested Json is provided in this link - https://github.com/Azure/usql/blob/master/Examples/JsonSample/JsonSample/NestedJsonParsing.usql.
You can use JsonTuple on the profile column to further extract specific nodes
E.g. use JsonTuple to get all the child nodes of the profile node and extract specific values like how you did in your code.
#childnodesofprofile =
SELECT
Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(profile) AS childnodes_map
FROM #result;
#values =
SELECT
childnodes_map["name"] AS name,
childnodes_map["id"] AS id
FROM #result;
Alternatively, if you are interested in specific values, you can also pass paramters to the JsonTuple function to get the specific nodes you want. The code below gets the locality node from the recursively nested nodes (as described by the "$..value" construct.
#locality =
SELECT Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(profile, "$..locality").Values AS locality
FROM #result;
Other supported constructs by JsonTuple
JsonTuple(json, "id", "name") // field names
JsonTuple(json, "$.address.zip") // nested fields
JsonTuple(json, "$..address") // recursive children
JsonTuple(json, "$[?(#.id > 1)].id") // path expression
JsonTuple(json) // all children
Hope this helps.