在 Postgresql 中提取 jsonb - 同一 jsonb 字段中的字典和数组结构
Extract jsonb in Postgresql - dictionary and array structure in same jsonb field
我正在使用 postgresql,我在数据集上有这个 table,有些列是 jsonb。
SELECT
external_id as cod,
title as name,
objectives
FROM table
因此,“目标”列的结果如下:
{"blocks":
[
{"key":"dek2k",
"text":"Objetivo Geral",
"type":"unstyled",
"depth":0,
"inlineStyleRanges":
[
{"offset":0,"length":14,"style":"fontsize-12pt"},
{"offset":0,"length":14,"style":"fontfamily-Arial"},
{"offset":0,"length":14,"style":"fontsize-14"}
],
"entityRanges":[],
"data":{"text-align":"start"}},
{"key":"ct1vn",
"text":"Conhecer e aplicar ferramentas para análise da mídias em sua respectiva relação com a cognição e o design,",
"type":"unstyled","depth":0,"inlineStyleRanges":
[
{"offset":0,"length":216,"style":"color-rgb(0,0,0)"},
{"offset":0,"length":216,"style":"fontsize-12pt"},
{"offset":0,"length":216,"style":"fontfamily-Arial"},
{"offset":0,"length":216,"style":"fontsize-14"}
],
"entityRanges":[],
"data":{}},
{"key":"8jshq",
"text":"","type":"unstyled","depth":0,
"inlineStyleRanges":[],
"entityRanges":[],"data":{}},
{"key":"avq4h",
"text":"tendo como ênfase os estudos das materialidades dos meios de comunicaçõe e seus aspectos sensoriais.",
"type":"unstyled",
"depth":0,
"inlineStyleRanges":
[
{"offset":0,"length":23,"style":"color-rgb(0,0,0)"},
{"offset":0,"length":23,"style":"fontsize-12pt"},
{"offset":0,"length":23,"style":"fontfamily-Arial"},
{"offset":0,"length":23,"style":"fontsize-14"}
],
"entityRanges":[],
"data":{}}
],
"entityMap":{}
}
我只想得到“文本”中的结果。有时它只有一个“文本”值,有时它有多达 15 个或更多。
我需要的是 table 中所有行的结果:
cod | name | objectives
----------+-----------------+----------
1 | A | Objetivo Geral Conhecer e aplicar ferramentas para análise da mídias em sua respectiva relação com a cognição e o design, tendo como ênfase os estudos das materialidades dos meios de comunicaçõe e seus aspectos sensoriais.
有线索吗?
json 函数与 string_agg()
一起实现你想要的。
使用 WITH ORDINALITY
保证 text
元素的正确排序。
SELECT t.external_id as cod,
t.title as name,
string_agg(a.block->>'text', ' ' ORDER BY rn) as objectives
FROM "table" t
CROSS JOIN LATERAL jsonb_array_elements(t.objectives->'blocks')
WITH ORDINALITY as a(block, rn)
GROUP BY t.external_id, t.title;
我正在使用 postgresql,我在数据集上有这个 table,有些列是 jsonb。
SELECT
external_id as cod,
title as name,
objectives
FROM table
因此,“目标”列的结果如下:
{"blocks":
[
{"key":"dek2k",
"text":"Objetivo Geral",
"type":"unstyled",
"depth":0,
"inlineStyleRanges":
[
{"offset":0,"length":14,"style":"fontsize-12pt"},
{"offset":0,"length":14,"style":"fontfamily-Arial"},
{"offset":0,"length":14,"style":"fontsize-14"}
],
"entityRanges":[],
"data":{"text-align":"start"}},
{"key":"ct1vn",
"text":"Conhecer e aplicar ferramentas para análise da mídias em sua respectiva relação com a cognição e o design,",
"type":"unstyled","depth":0,"inlineStyleRanges":
[
{"offset":0,"length":216,"style":"color-rgb(0,0,0)"},
{"offset":0,"length":216,"style":"fontsize-12pt"},
{"offset":0,"length":216,"style":"fontfamily-Arial"},
{"offset":0,"length":216,"style":"fontsize-14"}
],
"entityRanges":[],
"data":{}},
{"key":"8jshq",
"text":"","type":"unstyled","depth":0,
"inlineStyleRanges":[],
"entityRanges":[],"data":{}},
{"key":"avq4h",
"text":"tendo como ênfase os estudos das materialidades dos meios de comunicaçõe e seus aspectos sensoriais.",
"type":"unstyled",
"depth":0,
"inlineStyleRanges":
[
{"offset":0,"length":23,"style":"color-rgb(0,0,0)"},
{"offset":0,"length":23,"style":"fontsize-12pt"},
{"offset":0,"length":23,"style":"fontfamily-Arial"},
{"offset":0,"length":23,"style":"fontsize-14"}
],
"entityRanges":[],
"data":{}}
],
"entityMap":{}
}
我只想得到“文本”中的结果。有时它只有一个“文本”值,有时它有多达 15 个或更多。
我需要的是 table 中所有行的结果:
cod | name | objectives
----------+-----------------+----------
1 | A | Objetivo Geral Conhecer e aplicar ferramentas para análise da mídias em sua respectiva relação com a cognição e o design, tendo como ênfase os estudos das materialidades dos meios de comunicaçõe e seus aspectos sensoriais.
有线索吗?
json 函数与 string_agg()
一起实现你想要的。
使用 WITH ORDINALITY
保证 text
元素的正确排序。
SELECT t.external_id as cod,
t.title as name,
string_agg(a.block->>'text', ' ' ORDER BY rn) as objectives
FROM "table" t
CROSS JOIN LATERAL jsonb_array_elements(t.objectives->'blocks')
WITH ORDINALITY as a(block, rn)
GROUP BY t.external_id, t.title;