Extract JSON from a list in one of the columns in Delta table

Kumar, Arun 336 Reputation points
2023-12-28T20:21:54.85+00:00

I have a delta table with a column that have JSON data in the form of a list. I am trying to extract through JSON_VALUE, JSON_QUERY etc, but all giving a NULL value as output. For example my query looks like below -

SET ANSI_WARNINGS OFF
SELECT
id 
JSON_QUERY(data, '$.data1') AS test
JSON_VALUE(data,'$.data1[0]') AS test2
FROM
OPENROWSET(
BULK 'abc/int/tran/',
DATA_SOURCE = 'blob',
FORMAT='DELTA',
fieldterminator ='0x0b',
fieldquote = '0x0b'
) as test 


The column data has value like below -

[{"data1":293,"data1c":0.2069103293,"data1upc":{"unit_id":"123456","base1":"0.001","base2":"1000",.........

How to extract data1, data1c, base1, base2 etc?

Appreciate your help in advance

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,381 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,655 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bhargava-MSFT 31,261 Reputation points Microsoft Employee Moderator
    2024-01-05T00:25:42.78+00:00

    Hello Kumar, Arun,

    If the data stored in your table as a JSON array(but not a JSON object), then you need to set the index of the JSON object in the Json array.

    Ex:

    SELECT 
        id,
        JSON_VALUE(j.data, '$[0].data1') AS data1,
        JSON_VALUE(j.data, '$[0].data1c') AS data1c,
        JSON_VALUE(j.data, '$[0].data1upc.base1') AS base1,
        JSON_VALUE(j.data, '$[0].data1upc.base2') AS base2
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Loren Prifti 0 Reputation points
    2023-12-28T22:14:53.4066667+00:00

    Hi, try this and see if it works.

    1. CROSS APPLY OPENJSON(test.data) is used to parse the JSON data in each row into a new JSON object.
    2. WITH (data NVARCHAR(MAX) '$') AS j defines the structure of the JSON you're parsing. Here, it's treating each element in the JSON array as a separate JSON object aliased as j.
    3. JSON_VALUE(j.data, '$.data1') and so on are used to extract the values for each key from the JSON object.
    SELECT 
      id,
      JSON_VALUE(j.data, '$.data1') AS data1,
      JSON_VALUE(j.data, '$.data1c') AS data1c,
      JSON_VALUE(j.data, '$.data1upc.base1') AS base1,
      JSON_VALUE(j.data, '$.data1upc.base2') AS base2
    FROM 
      OPENROWSET(
        BULK 'abc/int/tran/',
        DATA_SOURCE = 'blob',
        FORMAT='DELTA',
        fieldterminator ='0x0b',
        fieldquote = '0x0b'
      ) AS test
    CROSS APPLY OPENJSON(test.data) WITH (data NVARCHAR(MAX) '$') AS j
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.