2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JSONの文字列を配列型に変換する(Presto)

Last updated at Posted at 2018-03-13

概要

下記のようなJSON形式の文字列を配列型に変換する方法を調べたのでメモしておく。

JSON型へキャストする

Prestoで、各値をJSON型に変換する。

SELECT 
  CAST(NULL AS JSON), -- NULL
  CAST(1 AS JSON),    -- JSON '1'
  CAST('abc' AS JSON),-- JSON '"abc"'
  CAST(true AS JSON), -- JSON 'true'
  CAST(1.234 AS JSON),-- JSON '1.234'
  CAST(ARRAY[1,2,3] AS JSON), -- JSON '[1,2,3]'
  CAST(ARRAY[1,NULL,3] AS JSON), -- JSON '[1,null,3]'
  CAST(ARRAY[ARRAY[1,2], ARRAY[3,4]] AS JSON), -- JSON '[[1,2],[3,4]]'
  CAST(MAP(ARRAY['k1','k2'], ARRAY[1,2]) AS JSON), -- JSON '{"k1":1,"k2":2}'
  json_parse('["a","b"]') -- JSON ["a","b"]
;

文字列型 > JSON型 > ARRAYに変換する

Prestoに「json_parse」という関数があったので文字列をJSON型にするのに利用する。

SELECT 
  CAST(json_parse('["a","b"]') AS ARRAY(VARCHAR)) -- JSON ["a","b"]
;

参考サイト

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?