<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Access data from JSON_TABLE with SQL in HANA in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/access-data-from-json-table-with-sql-in-hana/qaa-p/609694#M146200</link>
    <description>&lt;P&gt;Please can you show how you access the "forecasts" array within your JSON using the JSON_TABLE function, to be able to check what you make possibly wrong.&lt;/P&gt;</description>
    <pubDate>Tue, 13 Mar 2018 06:10:01 GMT</pubDate>
    <dc:creator>pfefferf</dc:creator>
    <dc:date>2018-03-13T06:10:01Z</dc:date>
    <item>
      <title>Access data from JSON_TABLE with SQL in HANA</title>
      <link>https://community.sap.com/t5/technology-q-a/access-data-from-json-table-with-sql-in-hana/qaq-p/609693</link>
      <description>&lt;P&gt;Maybe u come across a subject like this as well. &lt;/P&gt;
  &lt;P&gt;Transfer data from a big JSON to a relational table in HANA and second consume the stored information with SQL..&lt;/P&gt;
  &lt;P&gt;1. Create a table in HANA to store the JSON information.&lt;/P&gt;
  &lt;P&gt;2. Extract the data with SQL and to a result table. &lt;/P&gt;
  &lt;P&gt;Here is my solution:&lt;BR /&gt;&lt;BR /&gt;Given a JSON file below I want to access information by using some SQL in HANA.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;{
    "metadata": {
        "language": "de-DE",
        "transaction_id": "1511335024960:1650367502",
        "version": "1",
        "latitude": 37.54,
        "longitude": 5.58,
        "units": "m",
        "expire_time_gmt": 1511335872,
        "status_code": 200
    },
    "forecasts": [
        {
            "class": "fod_long_range_daily",
            "expire_time_gmt": 1511335872,
            "fcst_valid": 1511330400,
        
            "night": {
                "fcst_valid": 1511373600,
                "fcst_valid_local": "2017-11-22T19:00:00+0100",
                "day_ind" : "N"
            },

            "day": {
                "fcst_valid": 1511330400,
                "fcst_valid_local": "2017-11-22T07:00:00+0100",
                "day_ind" : "D"
            }
        }
    ]
}
');
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I created a table "CREATE TABLE &amp;lt;SCHEMA&amp;gt;.FoD (A INT, B CLOB);" in HANA and dumped the JSON to it. &lt;/P&gt;
  &lt;P&gt;The nested information can be accessed by using this SQL:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;-- select from an ordinality column with nested columns
SELECT *
FROM JSON_TABLE (
        &amp;lt;SCHEMA&amp;gt;.FoD.B, '$'
        
        COLUMNS (
            RN FOR ORDINALITY,
            language_code NVARCHAR(50) PATH '$.metadata.language',
            transaction_id NVARCHAR(50) PATH '$.metadata.transaction_id',
            version NVARCHAR(50) PATH '$.metadata.version',            
            latitude NVARCHAR(50) PATH '$.metadata.latitude',            
            longitude NVARCHAR(50) PATH '$.metadata.longitude',            
            units NVARCHAR(50) PATH '$.metadata.units',            
            expire_time_gmt NVARCHAR(50) PATH '$.metadata.expire_time_gmt',    
            status_code NVARCHAR(50) PATH '$.metadata.status_code',
            
-- Extract the information from section "forecasts"    
        
            NESTED PATH '$.forecasts[*]'
            COLUMNS (
                class NVARCHAR(50) PATH '$.class',
                
-- Extract the information from section "night" as part of section "forecasts"                        
                    NESTED PATH '$.night'
                        COLUMNS (
                            fcst_valid_local_night NVARCHAR(24) PATH '$.fcst_valid_local',
                            night_ind NVARCHAR(1) PATH '$.day_ind' ) 
                            
                            ,
-- Extract the information from section "day" as part of section "forecasts"            
                    NESTED PATH '$.day'
                        COLUMNS (
                            fcst_valid_local_day NVARCHAR(24) PATH '$.fcst_valid_local',
                            day_ind NVARCHAR(1) PATH '$.day_ind' )        
                                                        
            )
        )
) AS JT;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;To avoid multiple columns for "fcst_valid_local" with the extension "_day" and "_night" from above I used to combine extractions from the "day" and "night" section of "forecast" by using UNION.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;SELECT
     * 
FROM ( 

    SELECT
     * 
    FROM JSON_TABLE ( &amp;lt;SCHEMA&amp;gt;.FoD.B,
     '$' COLUMNS ( RN FOR ORDINALITY,
     language_code NVARCHAR(50) PATH '$.metadata.language',
     transaction_id NVARCHAR(50) PATH '$.metadata.transaction_id',
     version NVARCHAR(50) PATH '$.metadata.version',
     latitude NVARCHAR(50) PATH '$.metadata.latitude',
     longitude NVARCHAR(50) PATH '$.metadata.longitude',
     units NVARCHAR(50) PATH '$.metadata.units',
     expire_time_gmt NVARCHAR(50) PATH '$.metadata.expire_time_gmt',
     status_code NVARCHAR(50) PATH '$.metadata.status_code',
     -- Extract the information from section "forecasts"    
 NESTED PATH '$.forecasts[*]' COLUMNS ( class NVARCHAR(50) PATH '$.class',
     -- Extract the information from section "night" as part of section "forecasts"                        
 NESTED PATH '$.night' COLUMNS ( fcst_valid_local NVARCHAR(24) PATH '$.fcst_valid_local',
     ind NVARCHAR(1) PATH '$.day_ind' ) ) ) ) AS JT_night 
    
    UNION 
    
    SELECT
     * 
    FROM JSON_TABLE ( &amp;lt;SCHEMA&amp;gt;.FoD.B,
     '$' COLUMNS ( RN FOR ORDINALITY,
     language_code NVARCHAR(50) PATH '$.metadata.language',
     transaction_id NVARCHAR(50) PATH '$.metadata.transaction_id',
     version NVARCHAR(50) PATH '$.metadata.version',
     latitude NVARCHAR(50) PATH '$.metadata.latitude',
     longitude NVARCHAR(50) PATH '$.metadata.longitude',
     units NVARCHAR(50) PATH '$.metadata.units',
     expire_time_gmt NVARCHAR(50) PATH '$.metadata.expire_time_gmt',
     status_code NVARCHAR(50) PATH '$.metadata.status_code',
     -- Extract the information from section "forecasts"    
 NESTED PATH '$.forecasts[*]' COLUMNS ( class NVARCHAR(50) PATH '$.class',
     -- Extract the information from section "day" as part of section "forecasts"            
 NESTED PATH '$.day' COLUMNS ( fcst_valid_local NVARCHAR(24) PATH '$.fcst_valid_local',
     ind NVARCHAR(1) PATH '$.day_ind' ) ) ) ) AS JT_day ) 
  &lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Any idea how this can be achieved in an more elegant way?&lt;/P&gt;
  &lt;P&gt;Regards&lt;/P&gt;
  &lt;P&gt;Dio.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Mar 2018 20:45:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/access-data-from-json-table-with-sql-in-hana/qaq-p/609693</guid>
      <dc:creator>DirkO</dc:creator>
      <dc:date>2018-03-12T20:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: Access data from JSON_TABLE with SQL in HANA</title>
      <link>https://community.sap.com/t5/technology-q-a/access-data-from-json-table-with-sql-in-hana/qaa-p/609694#M146200</link>
      <description>&lt;P&gt;Please can you show how you access the "forecasts" array within your JSON using the JSON_TABLE function, to be able to check what you make possibly wrong.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Mar 2018 06:10:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/access-data-from-json-table-with-sql-in-hana/qaa-p/609694#M146200</guid>
      <dc:creator>pfefferf</dc:creator>
      <dc:date>2018-03-13T06:10:01Z</dc:date>
    </item>
  </channel>
</rss>

