简体   繁体   中英

Extract values from JSON in Oracle

I need to extract the value of marketDescription and channelName attributes as two different columns from the below JSON:

{
  'betType':'SYSTEM',
  'multiplier':1,
  'selections':[
    {
      'marketId':'/vfl/vf:match:1266330425/SCR/FT/1X2',
      'marketDescription':'Maç Sonucu',
      'betSelectionId':'/vfl/vf:match:1266330425/SCR/FT/1X2/1',
      'betSelectionDescription':'K. MAKEDONYA',
      'odds':50000,
      'event':{
        'eventId':'vf:match:1266330425',
        'channelType':'VFEC',
        'channelName':'Maç günü 3',
        'timestamp':'2021-07-16T10:44:43.000Z',
        'homeTeam':'K. MAKEDONYA',
        'awayTeam':'HOLLANDA'
      }
    },
    {
      'marketId':'/vfl/vf:match:1266330427/SCR/FT/1X2',
      'marketDescription':'Maç Sonucu',
      'betSelectionId':'/vfl/vf:match:1266330427/SCR/FT/1X2/1',
      'betSelectionDescription':'AVUSTURYA',
      'odds':24500,
      'event':{
        'eventId':'vf:match:1266330427',
        'channelType':'VFEC',
        'channelName':'Maç günü 3',
        'timestamp':'2021-07-16T10:44:43.000Z',
        'homeTeam':'AVUSTURYA',
        'awayTeam':'UKRAYNA'
      }
    },
    {
      'marketId':'/vfl/vf:match:1266330437/SCR/FT/1X2',
      'marketDescription':'Maç Sonucu',
      'betSelectionId':'/vfl/vf:match:1266330437/SCR/FT/1X2/1',
      'betSelectionDescription':'İSKOÇYA',
      'odds':33500,
      'event':{
        'eventId':'vf:match:1266330437',
        'channelType':'VFEC',
        'channelName':'Maç günü 3',
        'timestamp':'2021-07-16T10:44:43.000Z',
        'homeTeam':'İSKOÇYA',
        'awayTeam':'HIRVATİSTAN'
      }
    }
  ]
}

Expected result :

marketDescription   channelName 
Maç Sonucu          Maç günü 3

I tried to use JSON_VALUE command but I don't know how to extract them.

Given your JSON structure, you can use json_table rather than json_value :

select marketDescription, channelName
from json_table (
  <your_json_string>,
  '$.selections[*]'
  columns
    marketDescription varchar2(20) path '$.marketDescription',
    channelName varchar2(20) path '$.event.channelName'
)

Plugging in your example as a single line for brevity:

select marketDescription, channelName
from json_table (
  q'^{'betType':'SYSTEM','multiplier':1,'selections':[{'marketId':'/vfl/vf:match:1266330425/SCR/FT/1X2','marketDescription':'Maç Sonucu','betSelectionId':'/vfl/vf:match:1266330425/SCR/FT/1X2/1','betSelectionDescription':'K. MAKEDONYA','odds':50000,'event':{'eventId':'vf:match:1266330425','channelType':'VFEC','channelName':'Maç günü 3','timestamp':'2021-07-16T10:44:43.000Z','homeTeam':'K. MAKEDONYA','awayTeam':'HOLLANDA'}},{'marketId':'/vfl/vf:match:1266330427/SCR/FT/1X2','marketDescription':'Maç Sonucu','betSelectionId':'/vfl/vf:match:1266330427/SCR/FT/1X2/1','betSelectionDescription':'AVUSTURYA','odds':24500,'event':{'eventId':'vf:match:1266330427','channelType':'VFEC','channelName':'Maç günü 3','timestamp':'2021-07-16T10:44:43.000Z','homeTeam':'AVUSTURYA','awayTeam':'UKRAYNA'}},{'marketId':'/vfl/vf:match:1266330437/SCR/FT/1X2','marketDescription':'Maç Sonucu','betSelectionId':'/vfl/vf:match:1266330437/SCR/FT/1X2/1','betSelectionDescription':'İSKOÇYA','odds':33500,'event':{'eventId':'vf:match:1266330437','channelType':'VFEC','channelName':'Maç günü 3','timestamp':'2021-07-16T10:44:43.000Z','homeTeam':'İSKOÇYA','awayTeam':'HIRVATİSTAN'}}]}^' format json,
  '$.selections[*]'
  columns
    marketDescription varchar2(20) path '$.marketDescription',
    channelName varchar2(20) path '$.event.channelName'
)

gives

MARKETDESCRIPTION    CHANNELNAME         
-------------------- --------------------
Maç Sonucu           Maç günü 3          
Maç Sonucu           Maç günü 3          
Maç Sonucu           Maç günü 3          

It gets the values from each array element. They happen to be the same here, so you can just add distinct if you only want to see one of each combination.

with t as (
select q'[
{
  'betType':'SYSTEM',
  'multiplier':1,
  'selections':[
    {
      'marketId':'/vfl/vf:match:1266330425/SCR/FT/1X2',
      'marketDescription':'Mac Sonucu',
      'betSelectionId':'/vfl/vf:match:1266330425/SCR/FT/1X2/1',
      'betSelectionDescription':'K. MAKEDONYA',
      'odds':50000,
      'event':{
        'eventId':'vf:match:1266330425',
        'channelType':'VFEC',
        'channelName':'Mac gunu 3',
        'timestamp':'2021-07-16T10:44:43.000Z',
        'homeTeam':'K. MAKEDONYA','awayTeam':'HOLLANDA'
        }
    },
    {
      'marketId':'/vfl/vf:match:1266330427/SCR/FT/1X2',
      'marketDescription':'Mac Sonucu',
      'betSelectionId':'/vfl/vf:match:1266330427/SCR/FT/1X2/1',
      'betSelectionDescription':'AVUSTURYA',
      'odds':24500,
      'event':{
        'eventId':'vf:match:1266330427',
        'channelType':'VFEC',
        'channelName':'Mac gunu 3',
        'timestamp':'2021-07-16T10:44:43.000Z',
        'homeTeam':'AVUSTURYA',
        'awayTeam':'UKRAYNA'
      }
    },
    {
      'marketId':'/vfl/vf:match:1266330437/SCR/FT/1X2',
      'marketDescription':'Mac Sonucu',
      'betSelectionId':'/vfl/vf:match:1266330437/SCR/FT/1X2/1',
      'betSelectionDescription':'ISKOCYA',
      'odds':33500,
      'event':{
        'eventId':'vf:match:1266330437',
        'channelType':'VFEC',
        'channelName':'Mac gunu 3',
        'timestamp':'2021-07-16T10:44:43.000Z',
        'homeTeam':'ISKOCYA','awayTeam':'HIRVATISTAN'
      }
    }
  ]
}
]' js
from dual
)
select
    j.*
from t, 
    json_table(
      t.js, 
      '$.selections[*]'
      columns(
       n for ordinality
      ,marketId path '$.marketId'
      ,marketDescription path '$.marketDescription'
      ,channelName path '$.event.channelName'
      )
    ) j

Results:

         N MARKETID                            MARKETDESCRIPTION    CHANNELNAME
---------- ----------------------------------- -------------------- --------------------
         1 /vfl/vf:match:1266330425/SCR/FT/1X2 Mac Sonucu           Mac gunu 3
         2 /vfl/vf:match:1266330427/SCR/FT/1X2 Mac Sonucu           Mac gunu 3
         3 /vfl/vf:match:1266330437/SCR/FT/1X2 Mac Sonucu           Mac gunu 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM