Quantcast
Channel: SCN: Message List - SAP SQL Anywhere
Viewing all articles
Browse latest Browse all 2182

Re: Web service with JSON result

$
0
0

Hi Frank,

 

When I create a web service that returns JSON

 

Can you also provide the web service definition you're using? From what I can see, the result you have copied is just how it appears in Interactive SQL when copying a result set. The "forjson(...)" is just the column alias, and the actual data as it appears in an actual web service is not quoted as an entire value when actually being selected back out of an application.

 

Try this example:

 

CREATE OR REPLACE PROCEDURE ListJSONEmployees()
RESULT (json_result LONG VARCHAR)
BEGIN
    SELECT top 3
       emp.EmployeeID,
       so.CustomerID,
       so.Region
    FROM Employees AS emp KEY JOIN SalesOrders AS so WHERE emp.EmployeeID <= 195
    ORDER BY 1
    FOR JSON AUTO;
END;

CALL ListJSONEmployees();

 

You'll note the column in the result set has removed the "forjson" header is now just renamed to 'json_result'. If I call the service via a 'RAW' service:

 

CREATE SERVICE "jsonViaRawEmployeeList"

    TYPE 'RAW'

    AUTHORIZATION OFF

    SECURE OFF

    USER DBA

    AS CALL ListJSONEmployees();

 

And load "http://localhost/jsonViaRawEmployeeList" into a web browser, I get as an output:

 

[{"emp":{"EmployeeID":129,"so":[{"CustomerID":107,"Region":"Eastern"},{"CustomerID":119,"Region":"Western"},{"CustomerID":131,"Region":"Eastern"}]}}]

 

There are no extra quotes around the data. If I instead try a 'JSON' service, I don't need to use 'FOR JSON AUTO' when selecting data:

 

CREATE OR REPLACE PROCEDURE ListEmployees()
RESULT (
EmployeeID            integer,
CustomerID             integer,
Region             char(7) )
BEGIN
    SELECT top 3
       emp.EmployeeID,
       so.CustomerID,
       so.Region
    FROM Employees AS emp KEY JOIN SalesOrders AS so WHERE emp.EmployeeID <= 195
    ORDER BY 1
END;

CREATE SERVICE "jsonEmployeeList"
    TYPE 'JSON'
    AUTHORIZATION OFF
    SECURE OFF
    USER DBA
    AS CALL ListEmployees();

 

I then get via "http://localhost/jsonEmployeeList" into a web browser, I get as an output:

 

[

{

"EmployeeID": 129,

"Surname": "107",

"GivenName": "Eastern"

},

{

"EmployeeID": 129,

"Surname": "119",

"GivenName": "Western"

},

{

"EmployeeID": 129,

"Surname": "131",

"GivenName": "Eastern"

}

]

 

Hopefully that helps.

 

Regards,

 

Jeff Albion

SAP Active Global Support


Viewing all articles
Browse latest Browse all 2182

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>