Programming Languages on Power

Power Programming Languages

Connect, learn, share, and engage with IBM Power.


#Power
 View Only
  • 1.  SQL into JSON_TABLE from Webservice

    Posted Fri July 19, 2024 11:08 AM

    Hi Folks - I'm trying to get JSON data from:

    https://openholidaysapi.org/PublicHolidays?countryIsoCode=IE&languageIsoCode=EN&validFrom=2024-01-01&validTo=2024-12-31

    ...JSON Data is being returned as an array:

    [
      {
        "id": "a52297b3-caf7-48b6-bc68-03f973a2e8ce",
        "startDate": "2024-01-01",
        "endDate": "2024-01-01",
        "type": "Public",
        "name": [
          {
            "language": "EN",
            "text": "New Year's Day"
          }
        ],
        "nationwide": true
      },
      {
        "id": "09c316fe-6b3b-4f22-a988-dc2e6bb55e68",
        "startDate": "2024-02-05",
        "endDate": "2024-02-05",
        "type": "Public",
        "name": [
          {
            "language": "EN",
            "text": "Saint Brigid's Day"
          }
        ],
        "nationwide": true
      }]


     If I take the above data and load it into a variable:

    set json_var = 
    '[
    {
        "id": "a52297b3-caf7-48b6-bc68-03f973a2e8ce",
        "startDate": "2024-01-01",
        "endDate": "2024-01-01",
        "type": "Public",
        "name": [
          {
            "language": "EN",
            "text": "New Years Day"
          }
        ],
        "nationwide": true
      },
      {
        "id": "09c316fe-6b3b-4f22-a988-dc2e6bb55e68",
        "startDate": "2024-02-05",
        "endDate": "2024-02-05",
        "type": "Public",
        "name": [
          {
            "language": "EN",
            "text": "Saint Brigids Day"
          }
        ],
        "nationwide": true
      },
      {
        "id": "45bca84d-02e9-41b3-bf97-53c4e4892b12",
        "startDate": "2024-03-17",
        "endDate": "2024-03-17",
        "type": "Public",
        "name": [
          {
            "language": "EN",
            "text": "Saint Patricks Day"
          }
        ],
        "nationwide": true
      }
      ]'
    ;

    ...I'm able to parse the results:

    SELECT *
    FROM json_table(json_var, 'lax $[*]' 
                            COLUMNS(
                            id VARCHAR(255path 'lax $.id',
                            startDate VARCHAR(255path 'lax $.startDate',
                            endDate VARCHAR(255path 'lax $.endDate',
                            type VARCHAR(255path 'lax $.type',
                            name varchar(20path 'lax $.name.text'
                            ) error on error
                    );

     ==>

    ...I had to remove the apostrophe characters from the data but was able to get the expected results. However - when I replace the json_var with the actual API

    SELECT *
    FROM json_table('http://openholidaysapi.org/PublicHolidays?countryIsoCode=IE&languageIsoCode=EN&validFrom=2024-01-01&validTo=2024-12-31''lax $[*]' 
                            COLUMNS(
                            id VARCHAR(255path 'lax $.id',
                            startDate VARCHAR(255path 'lax $.startDate',
                            endDate VARCHAR(255path 'lax $.endDate',
                            type VARCHAR(255path 'lax $.type',
                            name varchar(20path 'lax $.name.text'
                            ) error on error
                    );

    ...and run it, I get the following:

    ...I am assuming that the issue is because of the apostrophes in the data...can someone please advise me on what I am doing wrong, and what I need to do to get this working?

    Many Thanks

    Paul



    ------------------------------
    Paul Archer
    ------------------------------

    #SQL


  • 2.  RE: SQL into JSON_TABLE from Webservice

    Posted Sun July 21, 2024 01:40 AM
    Edited by Daniel Gross Sun July 21, 2024 01:46 AM

    Hi Paul,

    the error results NOT from the apostrophes - you had to remove them, because the string-constant uses '...' as quotes - so to have a single ' in a string-constant you had to replace it with two single quotes '' (those are not a double quote but two single quotes) like in '... "Saint Patrick''s Day" ...'. But that only as short excursion to quoting string constants in SQL. 

    Now to something completely different... the JSON_TABLE function needs a JSON string as the first parameter - but you are feeding it an URL string. 

    JSON_TABLE does NOT retrieve the JSON from the web service by itself. You will have to use QSYS2.HTTP_GET function to do that for you.

    Here an example:

      SELECT *
      FROM json_table(
      qsys2.http_get('http://openholidaysapi.org/PublicHolidays?countryIsoCode=IE&languageIsoCode=EN&validFrom=2024-01-01&validTo=2024-12-31')
      ,
      'lax $[*]'
      COLUMNS(
      ...


      This should work - simply wrap that URL in a QSYS2.HTTP_GET function call. It will return the JSON string needed by the JSON_TABLE function.

      If you want a complete example on how to do that, I wrote a piece on my blog a while ago: https://qpgmr.de/ibmi-sql-json_table

      HTH and kind regards,

      Daniel



      ------------------------------
      Daniel Gross
      ------------------------------



    1. 3.  RE: SQL into JSON_TABLE from Webservice

      Posted Wed July 24, 2024 02:06 PM

      Hi Paul,

      it seems I cannot reply to your private massage (there is no reply button).

      The problem seems to be, that you have to use "https:" instead of "http:".

      The "http:" URL only returns a HTML answer "301 - Moved permanently" - HTTP_GET doesn't follow the redirection and returns the HTML string - and JSON_TABLE cannot handle this.

      When debugging SQL you always have to work inside-out / bottom-up - so I tried "values qsys2.http_get('http://...);" and got the HTML 301 as result. So I tried with "https://..." and it returned JSON.

      HTH

      Daniel



      ------------------------------
      Daniel Gross
      ------------------------------



    2. 4.  RE: SQL into JSON_TABLE from Webservice

      Posted Fri July 26, 2024 09:32 AM

      Hi Daniel

      There is still something wrong at my end - I am glad that it's working for you though - when I go through the same HTTPS process I am still getting errors.

      We are running at a 00 TR level for V7R3 so I am hoping that an upcoming OS update will resolve the problems. In the meantime I would like to thank you for taking the time to support my inquiry. You've been extremely patient and informative - I have learned a lot from this interaction.

      With Sincere Thanks & Best Regards



      ------------------------------
      Paul Archer
      ------------------------------



    3. 5.  RE: SQL into JSON_TABLE from Webservice

      Posted Sun July 28, 2024 10:16 AM

      Hi Paul,

      sorry I was traveling the last few days, so I couldn't respond. 

      I don't know exactly when the QSYS2.HTTP_GET functions was introduced - but before that, there was SYSTOOLS.HTTP_GET - the SYSTOOLS versions were much slower, because they were implemented in Java - but I'm quite sure, they were available on every 7.3 version and PTF level.

      Just try - it should work - even as it might be not so fast. 

      HTH



      ------------------------------
      Daniel Gross
      ------------------------------



    4. 6.  RE: SQL into JSON_TABLE from Webservice

      Posted Mon July 29, 2024 02:19 AM
      Edited by Satid S Mon July 29, 2024 02:27 AM

      Dear Paul

      For you to use the new HTTP_GET in SYS2 (as opposed to the old one in SYSTOOLS) in IBM i 7.3, you need to apply at least SF99703 Level 28 according to this IBM Technote: New HTTP functions based in QSYS2 at https://www.ibm.com/support/pages/new-http-functions-based-qsys2       

      This article provides samples of the use of these SQL HTTP functions: New QSYS2.HTTP Functions SQL (7.4 TR5 and 7.3 TR11) at https://blog.faq400.com/en/db2-for-i/qsys2-http-functions-en/     



      ------------------------------
      Satid S
      ------------------------------



    5. 7.  RE: SQL into JSON_TABLE from Webservice

      Posted Tue August 06, 2024 10:05 AM

      Hi Satid

      Thank you! 

      I think I have what I need for now - just have to get the ball rolling on the OS update!



      ------------------------------
      Paul Archer
      ------------------------------