简体   繁体   English

如何将JSON格式的oracle表行值转换为多个关系COLUMNS?

[英]How to convert JSON format oracle table row values into multiple relational COLUMNS?

I have a oracle table that has been loaded by MQ and one of the table column has values with JSON format data.I need to convert those JSON format data into relational row format. 我有一个由MQ加载的oracle表,其中一个表列具有JSON格式数据的值。我需要将这些JSON格式数据转换为关系行格式。 I am wondering if there is any SQL in oracle with/without creating temp tables to convert those JSON format data into relational column values? 我想知道在oracle中是否有任何SQL创建临时表以将这些JSON格式数据转换为关系列值?

Select JSON_MG from test_1;

JSON_MG
{
  "type": "testeevnet",
  "version": "test-1.0",
  "testsource": "1.0.0",
  "timestamp": "2019-02-14T20:45:18.4422201+00:00",
  "test_ID": 11,
  "tag": "22",
  "PNAME": "test/test_n",
  "ticket": "WT9999",
  "ticketStatus": "active",
  "tickets": [
    {
      "ticket": "convert",
      "code": "PA",
      "date": "2019-03-31"
    }
    }
  ]
}

-- Expected result all the JSON elements as columns - 预期结果将所有JSON元素作为列

json_table is what you're looking for. json_table是你要找的。 This can do JSON -> relational conversion. 这可以做JSON - >关系转换。

Just list out the paths to the elements you want to extract & their data type: 只需列出要提取的元素的路径及其数据类型:

with jdata as (
  select '{
  "type": "testeevnet",
  "version": "test-1.0",
  "testsource": "1.0.0",
  "timestamp": "2019-02-14T20:45:18.4422201+00:00",
  "test_ID": 11,
  "tag": "22",
  "PNAME": "test/test_n",
  "ticket": "WT9999",
  "ticketStatus": "active",
  "tickets": [
    {
      "ticket": "convert",
      "code": "PA",
      "date": "2019-03-31"
    }
  ]
}' doc
  from dual
)
  select t.* 
  from   jdata, json_table (
    doc, '$'
    columns (
      type    varchar2 path '$.type',
      version varchar2 path '$.version',
      nested  path '$.tickets[*]' 
      columns (
        ticket varchar2 path '$.ticket'
      )
    )
  ) t;

TYPE         VERSION    TICKET    
testeevnet   test-1.0   convert   

The nested path splits out the array elements into rows. nested path将数组元素拆分为行。 So as you add more objects in your tickets array, they become rows: 因此,当您在票证数组中添加更多对象时,它们将成为行:

with jdata as (
  select '{
  "type": "testeevnet",
  "version": "test-1.0",
  "testsource": "1.0.0",
  "timestamp": "2019-02-14T20:45:18.4422201+00:00",
  "test_ID": 11,
  "tag": "22",
  "PNAME": "test/test_n",
  "ticket": "WT9999",
  "ticketStatus": "active",
  "tickets": [
    {
      "ticket": "convert",
      "code": "PA",
      "date": "2019-03-31"
    },
    {
      "ticket": "convert2",
      "code": "PA",
      "date": "2019-03-31"
    }
  ]
}' doc
  from dual
)
  select t.* 
  from   jdata, json_table (
    doc, '$'
    columns (
      type    varchar2 path '$.type',
      version varchar2 path '$.version',
      nested  path '$.tickets[*]' 
      columns (
        ticket varchar2 path '$.ticket'
      )
    )
  ) t;

TYPE         VERSION    TICKET     
testeevnet   test-1.0   convert     
testeevnet   test-1.0   convert2 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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