JSON 関数¶
JSON 関数は、 json
型の値を作成および変換します。
次の関数が JSON 関数です。
AVRO_TO_JSON¶
説明
AVRO_TO_JSON
関数は AVRO ドキュメントと JSON スキーマから JSON ドキュメントを返します。この関数は、JSON 形式のデータや、AVRO を情報交換形式として使用するデータソースを扱う場合に非常に便利です。
構文
AVRO_TO_JSON( <AVRO document:blob>, <JSON schema:text> ):text
AVRO document
: 必須。JSON 形式にデシリアライズされる AVRO ドキュメントを含む Blob。JSON schema
: 必須。JSON ドキュメントの構造を指定する JSON スキーマを含むリテラル。
パラメータ AVRO document または JSON schema が null の場合、この関数は null を返します。また、デシリアライズ中に何らかの問題が発生した場合は null を返します。
例
例に必要なビュー
わかりやすくなるように、スキーマで 2 つのビューを作成し、JSON_TO_AVRO 関数を使用して AVRO ドキュメントを生成します。
CREATE VIEW schemaView1 as SELECT '{
"type":"record",
"name":"Person",
"fields":
[
{ "name":"name", "type":"string" },
{ "name":"age", "type":"int" }
]
}' AS schema FROM Dual();
CREATE VIEW schemaView2 as SELECT '{
"name": "subElements",
"type":"record",
"fields":
[
{
"name":"people",
"type":
{
"type": "array",
"items":
{
"name":"Person",
"type":"record",
"fields":
[
{"name":"name", "type":"string"},
{"name":"age", "type":"int"}
]
}
}
}
]
}' AS schema FROM Dual();
例 1
select AVRO_TO_JSON(
JSON_TO_AVRO('
{
"name":"Frank",
"age":47
}'
,schema)
,schema) from schemaView1;
avro_to_json |
---|
{"name":"Frank","age":47} |
例 2
SELECT AVRO_TO_JSON(
JSON_TO_AVRO('
{
"people":
[
{"name": "Frank", "age": 47},
{"name": "Maria", "age": 35}
]
}'
,schema)
,schema) from schemaView2;
avro_to_json |
---|
{"people":[{"name":"Frank","age":47},{"name":"Maria","age":35}]} |
COMPLEX_TYPE_TO_JSON¶
説明
COMPLEX_TYPE_TO_JSON
関数は、 register
や array
を JSON ドキュメントで text
値に変換します。
この関数には 2 つの署名があります。
構文 1
COMPLEX_TYPE_TO_JSON( <array value:array>, <JSON document:text>):text
array value
: 必須。値が JSON の生成に使用されるarray
。JSON text
: 必須。生成される JSON テキストの名前。
例 1
SELECT complex_type_to_json(
{ ROW (
'George',
'Washington',
'1732-02-22',
ROW ('3200 Mount Vernon Memorial Highway', 'Mount Vernon', 'Virginia', 'United States')
) },
'values'
)
FROM dual();
complex_type_to_json_results |
---|
{"values":[{"value":"George","value1":"Washington","value2":"1732-02-22", "value3":{"value":"3200 Mount Vernon Memorial Highway", "value1":"Mount Vernon","value2":"Virginia","value3":"United States"}}]} |
構文 2
COMPLEX_TYPE_TO_JSON( <complex type:register>):text
complex type
: 必須。値が JSON の生成に使用されるregister
。
例 1
以下のスキーマを持つ usa_president
というビューがあるとします。

select complex_type_to_json(person) from usa_president
complex_type_to_json_results |
---|
{"value":"George","value1":"Washington","value2":"1732-02-22", "value3":{"value":"3200 Mount Vernon Memorial Highway", "value1":"Mount Vernon","value2":"Virginia","value3":"United States"}} |
JSONPATH¶
説明
JSONPATH
関数は、JSONPath 式 (JSONPath API) によって選択された JSON ドキュメントからノードを返します。JSONPath 式は、XML ドキュメントの XPath 式に類似します。この関数は、データソースから JSON 形式のデータを扱うときに非常に便利で、このデータへのアクセスが容易になるさまざまな式を使用して情報をフィルタリングすることができます。
構文
JSONPATH( <JSON document:text>, <JSONPath expression:text> [ , <jsonOutput:boolean> ] ):text
JSON document
: 必須。JSONPath 式を適用したい JSON ドキュメントを含むリテラル。JSONPath expression
: 必須。JSONPath 式。jsonOutput
: オプション。ブール値で、その値が true の場合、関数は String を返し、二重引用符で囲みます。値が false の場合、動作はこのパラメータが渡されない場合と同じになります。
例
例 1
SELECT jsonpath('
{
"store":{
"book":[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Evelyn Waugh",
"title":"Sword of Honour",
"price":12.99
},
{
"category":"fiction",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
}
]
}
}
','$.store..author') as jsonpath_results
from Dual();
jsonpath_results |
---|
["Nigel Rees","Evelyn Waugh","Herman Melville"] |
例 2
SELECT jsonpath('
{
"store":{
"book":[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Evelyn Waugh",
"title":"Sword of Honour",
"price":12.99
},
{
"category":"fiction",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
}
]
}
}
','$.store.book[?(@.price < 10)]') as jsonpath_results
from Dual();
jsonpath_results |
---|
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
]
|
例 3
SELECT jsonpath('
{
"store":{
"book":[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Evelyn Waugh",
"title":"Sword of Honour",
"price":12.99
},
{
"category":"fiction",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
}
]
}
}
','$..book.length()') as jsonpath_results
from Dual();
jsonpath_results |
---|
3 |
3 つのすべての例で、同じ JSON 値が使用されます。この JSON 値には、本などの店に関する情報が含まれます。
例 4
SELECT jsonpath('
{
"store":{
"book":[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Evelyn Waugh",
"title":"Sword of Honour",
"price":12.99
},
{
"category":"fiction",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
}
]
}
}
','$..book[0].category') as jsonpath_results
from Dual();
jsonpath_results |
---|
reference |
例 5
SELECT jsonpath('
{
"store":{
"book":[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Evelyn Waugh",
"title":"Sword of Honour",
"price":12.99
},
{
"category":"fiction",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
}
]
}
}
','$..book[0].category', true) as jsonpath_results
from Dual();
jsonpath_results |
---|
"reference" |
すべての例で、同じ JSON 値が使用されます。この JSON 値には、本などの店に関する情報が含まれます。「例1」では、店内の本の著者を取得します。「例2」では、価格が 10 未満の本を取得します。「例3」では、店にある本の数を取得します。「例4」と「例5」では、デフォルトの動作と、オプションのパラメータ jsonOutput を使用した場合の違いがわかります。
JSON_TO_AVRO¶
説明
JSON_TO_AVRO
関数は JSON ドキュメントとスキーマから AVRO ドキュメントを返します。この関数は、JSON 形式のデータや、AVRO を情報交換形式として使用するデータソースを扱う場合に非常に便利です。
構文
JSON_TO_AVRO( <JSON document:text>, <JSON schema:text> ):blob
JSON document
: 必須。AVRO 形式にシリアル化される JSON ドキュメントを含むリテラル。JSON schema
: 必須。JSON ドキュメントの構造を指定する JSON スキーマを含むリテラル。
JSON document または JSON schema が null の場合は null を返します。また、シリアル化の間に何らかの問題が発生した場合は null を返します。
例
例 1
SELECT json_to_avro('
{
"name":"Frank",
"age":47
}
','
{
"type":"record",
"name":"Person",
"fields":
[
{ "name":"name", "type":"string" },
{ "name":"age", "type":"int" }
]
}') as json_to_avro_results from Dual();
json_to_avro_results |
---|
[BINARY DATA] - 7 bytes |
例 2
SELECT json_to_avro('
{
"people":
[
{"name": "Frank", "age": 47},
{"name": "Maria", "age": 35}
]
}','
{
"name": "subElements",
"type":"record",
"fields":
[
{
"name":"people",
"type":
{
"type": "array",
"items":
{
"name":"Person",
"type":"record",
"fields":
[
{"name":"name", "type":"string"},
{"name":"age", "type":"int"}
]
}
}
}
]
}') as json_to_avro_results from Dual();
json_to_avro_results |
---|
[BINARY DATA] - 16 bytes |
JSON_TO_COMPLEX_TYPE¶
説明
JSON_TO_COMPLEX_TYPE
関数は、JSON 式をレジスターの複合型に変換します。この関数では、JSON テキストとその JSON スキーマ表現の 2 つの文字列のパラメータが必要です。
構文
JSON_TO_COMPLEX_TYPE( <JSON document:text>, <JSON schema:text> ):text
JSON document
: 必須。 レジスターの複合型に変換する JSON 式を含むリテラル。JSON schema
: 必須。JSON ドキュメントの構造を指定する JSON スキーマを含むリテラル。
JSON schema では、patternProperties キーも itemPrefix キーも使用できません。代わりに properties キーと items キーを使用します。$schema の有効なバージョンは、 2019-19、V7、V6 e V4 です。
例 1
SELECT json_to_complex_type('{
"firstName": "John",
"lastName": "Doe",
"age": 21
}
',
'{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}') as result from dual();
json_to_complex_type_results |
---|
{"firstName":"John","lastName":"Doe","age":21} |