postgresql JSONB函数检索数组的问题

ugmeyewa  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(3)|浏览(229)

在一个名为temporay_data的表中,也有一个名为temporary_data的数据字段,填充有以下JSON结构

{
 "FormPayment": {
        "student": [
            {
                "fullname": "name student1 ",
                "rate": 210,
                "meal": 7,
                "mealValue": 175,
                "finalValue": 385,
                "role": "student",
                "willPay": true
            },
            {
                "fullname": "name student2",
                "rate": 210,
                "meal": 7,
                "mealValue": 175,
                "finalValue": 385,
                "role": "student",
                "willPay": true
            },
            {
                "fullname": "name student3",
                "rate": 210,
                "meal": 7,
                "mealValue": 175,
                "finalValue": 385,
                "role": "student",
                "willPay": true
            }
        ],
        "advisor": [
            {
                "fullname": "name advisor",
                "rate": 210,
                "meal": 7,
                "mealValue": 175,
                "finalValue": 385,
                "role": "advisor",
                "isParticipant": "yes",
                "willPay": true
            }
        ],
        "coadvisors": [
            {
                "fullname": "name coadvisors 1",
                "rate": 210,
                "meal": 7,
                "mealValue": 175,
                "finalValue": 385,
                "role": "coadvisor",
                "isParticipant": "yes",
                "willPay": true
            },
            {
                "fullname": "name coadvisors 2",
                "rate": 210,
                "meal": 7,
                "mealValue": 175,
                "finalValue": 385,
                "role": "coadvisor",
                "isParticipant": "no",
                "willPay": false
            }
        ]
    }
}

字符串
我需要选择所有的全名,我知道这是JSON中的一个数组。我尝试了上面的代码(和其他各种代码)

SELECT elements->>'fullname' as fullname
FROM (
    SELECT jsonb_array_elements(temporary_data->'FormPayment'->'student') as elements
    FROM temporary_data
) subquery;

and return this error 

ERROR:  function jsonb_array_elements(json) does not exist
LINE 31:     SELECT jsonb_array_elements(temporary_data->'FormPayment...
                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 687


安装了Jsonb,并安装了函数jsonb_array_elements和jsonb_array_elements_text。
我都试过了

SELECT elements->>'fullname' as fullname
FROM (
    SELECT jsonb_array_elements(temporary_data->'FormPayment'->'student') as elements
    FROM temporary_data

    UNION

    SELECT jsonb_array_elements(temporary_data->'FormPayment'->'coadvisors') as elements
    FROM temporary_data
) subquery;
</code>
<code>
SELECT jsonb_array_elements(temporary_data->'FormPayment'->'student'->'{fullname}'::jsonb[]) as elements
FROM temporary_data;
</code>
<code>
SELECT jsonb_array_elements(temporary_data->'FormPayment'->'student')->>'fullname' as fullname
FROM temporary_data;

ttygqcqt

ttygqcqt1#

错误:函数jsonb_array_elements(json)不存在
解决方案很简单:
使用函数json_array_elements()或传入jsonb(而不是json)。

ruoxqz4g

ruoxqz4g2#

坦克欧文Brandstetter.我与你的帮助下解决了,现在我发布的解决方案.

SELECT subquery.project_id,
    subquery.elements ->> 'fullname'::text AS fullname,
    subquery.elements ->> 'meal'::text AS meal,
    subquery.elements ->> 'role'::text AS tipo
   FROM ( SELECT json_array_elements((td.temporary_data -> 'FormPayment'::text) -> 'student'::text) AS elements,
            p.project_id
           FROM temporary_data td
             JOIN projects p ON td.id = p.temporary_data_id
          WHERE p.mostratec_id = 6 AND (p.status_id = ANY (ARRAY[4, 5]))) subquery

字符串

oo7oh9g9

oo7oh9g93#

对于这样一个复杂的JSON结构,我将使用JSONPATH查询:

SELECT *
FROM jsonb_path_query(
        CAST (/* your JSON */ AS jsonb),
        '$.**.fullname'
     );

字符串

相关问题