错误:函数 jsonb_array_elements_text(jsonb[]) 不存在

发布于 2025-01-20 06:20:25 字数 17695 浏览 2 评论 0原文

很难遍历和查询 jsonb[] 行中的元素。

CREATE TABLE foo (
  id uuid PRIMARY KEY,
  work_experience jsonb[] NOT NULL
);

INSERT INTO foo (id, work_experience)
VALUES (
  'b4e942a0-49b4-4fa7-8f7a-5fbf0541d1c9',
  E'{"{\\"id\\": \\"7cd74bae-ff5b-4f58-ab20-0218f820ffff\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"Scripting languages\\", \\"level3\\": \\"TypeScript\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"313384d2-486d-4b7f-ba33-76b1cd696f0a\\", \\"level3_id\\": \\"fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\"}, {\\"id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|686af7e4-6d58-4148-b227-3bf65ff10273\\", \\"level1\\": \\"Software frameworks & libraries\\", \\"level2\\": \\"Frontend frameworks & libraries\\", \\"level3\\": \\"React\\", \\"level1_id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b\\", \\"level2_id\\": \\"e13b48c1-fd0f-4ecc-89de-58e9312b9435\\", \\"level3_id\\": \\"686af7e4-6d58-4148-b227-3bf65ff10273\\"}, {\\"id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b|c4e54726-7bd5-44bb-8597-a05eb2272e2b|cda4441f-dba6-495c-9e2e-7429bd5e0465\\", \\"level1\\": \\"Software frameworks & libraries\\", \\"level2\\": \\"Backend frameworks & libraries\\", \\"level3\\": \\"Node.js\\", \\"level1_id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b\\", \\"level2_id\\": \\"c4e54726-7bd5-44bb-8597-a05eb2272e2b\\", \\"level3_id\\": \\"cda4441f-dba6-495c-9e2e-7429bd5e0465\\"}, {\\"id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\", \\"level1\\": \\"Software frameworks & libraries\\", \\"level2\\": \\"Frontend frameworks & libraries\\", \\"level3\\": \\"TypeScript\\", \\"level1_id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b\\", \\"level2_id\\": \\"e13b48c1-fd0f-4ecc-89de-58e9312b9435\\", \\"level3_id\\": \\"fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\"}], \\"end_date\\": null, \\"position\\": \\"Senior Software Engineer + Team Lead\\", \\"start_date\\": \\"2019-10-01T00:00:00\\", \\"description\\": \\"Draper, Utah, United States\\\\n• Architected Expert Portal* from the ground up using a Node/Typescript\\\\nbackend, a\\\\nPostgreSQL database, a GraphQL API layer, a Webpack build process, with a\\\\nTypescript/React front-end and XState for state management\\\\n• Enforced coding best practices with linting rules and code formatters by\\\\nautomating it in\\\\ngit workflow\\\\n• Automated deployment Expert Portal* to EC2 instances and the @pluralsight\\\\nNPM\\\\nartifactory using Github, TeamCity, and Octopus\\\\n• Improved product team workflow by building a browser extention to add\\\\nLeanKit card\\\\ntemplate functionality\\\\n• Consumed and published data through Kafka streams and RabbitMQ\\\\nmessages\\\\n• Interviewed, onboarded, and trained junior to mid-level engineers\\", \\"company_name\\": \\"Pluralsight\\"}","{\\"id\\": \\"9e2c2b44-39a4-4369-b237-c51fd938e61d\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"Scripting languages\\", \\"level3\\": \\"JavaScript\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"313384d2-486d-4b7f-ba33-76b1cd696f0a\\", \\"level3_id\\": \\"012abcd1-3a6a-4803-a47e-42f46b402024\\"}], \\"end_date\\": \\"2019-10-01T00:00:00\\", \\"position\\": \\"Software Engineer II\\", \\"start_date\\": \\"2017-11-01T00:00:00\\", \\"description\\": \\"Greater Salt Lake City Area\\\\nWorked on the KSL Jobs Classifieds team as a full-stack developer. Following\\\\nthe scrum methodology, I added new features and maintained all things\\\\npowered by KSL Jobs.\\\\n• Built and deployed a \\\\\\"white label\\\\\\" version of KSL Jobs for the Silicon Slopes\\\\nbrand. (https://siliconslopes.ksl.com)\\\\n• Rewrote major sections of the current KSL Jobs site in React.js\\\\n• Automated querying data for reports and analytic purposes through Node and\\\\nPHP scripts\\\\n• Provided rich data tracking through Google Tag Manager, Google Analytics,\\\\nand BigQuery\\\\n• Migrated Solr search engine to ElasticSearch with a GraphQL API\\", \\"company_name\\": \\"Deseret Digital Media\\"}","{\\"id\\": \\"efbf68f4-7bdc-4ab6-bba9-fbf7ec38aeef\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"High-level languages\\", \\"level3\\": \\"PHP\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"f45abc59-1e7e-4853-a498-999fcf12d498\\", \\"level3_id\\": \\"4b266297-6e25-4443-90ec-248bded4225a\\"}], \\"end_date\\": \\"2019-08-01T00:00:00\\", \\"position\\": \\"Full Stack Developer\\", \\"start_date\\": \\"2017-01-01T00:00:00\\", \\"description\\": \\"Provo, Utah Area\\\\nWorked with Appritech LLC to modernize their legacy software and add new\\\\nfeatures to automate their business processes.\\\\n•  Building new call handler from the ground up using JavaScript ES6,\\\\nBootstrap, SASS for\\\\nfront-end, and PHP7/Laravel for back-end\\\\n•  Implemented real-time call management system and built reporting API.\\\\nImproved call agent\\\\nproductivity by 70%\\\\n•  Upgraded deprecated PHP legacy code to PHP 7\\\\n•  Synchronized follow up calls with Twilio API functionality for SMS message\\\\nforwarding\\\\n•  Installed and setup Apache server for an after hours call center\\", \\"company_name\\": \\"Appritech Software\\"}","{\\"id\\": \\"2db60c6c-c214-4d9b-9034-baba676203a8\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"High-level languages\\", \\"level3\\": \\"PHP\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"f45abc59-1e7e-4853-a498-999fcf12d498\\", \\"level3_id\\": \\"4b266297-6e25-4443-90ec-248bded4225a\\"}], \\"end_date\\": \\"2017-09-01T00:00:00\\", \\"position\\": \\"Web Developer\\", \\"start_date\\": \\"2017-02-01T00:00:00\\", \\"description\\": \\"Provo, Utah Area\\\\nWorked with Redcore LLC to build entrepreneurial tools, marketing tools, and\\\\nadding new functionality to the current Wordpress-integrated website and\\\\nCMS.\\\\n• Built Brand Management website from the ground up using Bootstrap and\\\\nJavaScript on the\\\\nfront end, with PHP and MySQL on the back-end\\\\n• Automated managerial and accounting tasks, such as invoice generation,\\\\nand transactions using Stripe API\\\\n• Debugged and perfected current web applications to enrich UX\\\\n• Doubled clientele by expanding Redcore services offered to include website\\\\ncreation and\\\\nmanagement\\", \\"company_name\\": \\"Redcore LLC\\"}","{\\"id\\": \\"c3f1d5b2-5586-477d-ae4c-e2927463244e\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"Scripting languages\\", \\"level3\\": \\"JavaScript\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"313384d2-486d-4b7f-ba33-76b1cd696f0a\\", \\"level3_id\\": \\"012abcd1-3a6a-4803-a47e-42f46b402024\\"}], \\"end_date\\": \\"2017-03-01T00:00:00\\", \\"position\\": \\"Software and Web Developer\\", \\"start_date\\": \\"2016-12-01T00:00:00\\", \\"description\\": \\"Provo, Utah Area\\\\nThis internship was focused on building core website and software for\\\\nCentaurific using the LAMP stack.\\\\n• Created dynamic landing pages for products to generate leads\\\\n• Designed a MySQL database to organize data and generate excel reports\\\\n• Revitalized marketing efforts through analytics and a social media sharing\\\\npage\\", \\"company_name\\": \\"Centaurific\\"}"}'
);

(这是 work_experience 行在 JSON 中的样子:)

[
    {
        "id": "7cd74bae-ff5b-4f58-ab20-0218f820ffff",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da",
                "level1": "Programming languages",
                "level2": "Scripting languages",
                "level3": "TypeScript",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "313384d2-486d-4b7f-ba33-76b1cd696f0a",
                "level3_id": "fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"
            },
            {
                "id": "84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|686af7e4-6d58-4148-b227-3bf65ff10273",
                "level1": "Software frameworks & libraries",
                "level2": "Frontend frameworks & libraries",
                "level3": "React",
                "level1_id": "84dff39f-2ed6-408c-a188-14cf55a09c5b",
                "level2_id": "e13b48c1-fd0f-4ecc-89de-58e9312b9435",
                "level3_id": "686af7e4-6d58-4148-b227-3bf65ff10273"
            },
            {
                "id": "84dff39f-2ed6-408c-a188-14cf55a09c5b|c4e54726-7bd5-44bb-8597-a05eb2272e2b|cda4441f-dba6-495c-9e2e-7429bd5e0465",
                "level1": "Software frameworks & libraries",
                "level2": "Backend frameworks & libraries",
                "level3": "Node.js",
                "level1_id": "84dff39f-2ed6-408c-a188-14cf55a09c5b",
                "level2_id": "c4e54726-7bd5-44bb-8597-a05eb2272e2b",
                "level3_id": "cda4441f-dba6-495c-9e2e-7429bd5e0465"
            },
            {
                "id": "84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da",
                "level1": "Software frameworks & libraries",
                "level2": "Frontend frameworks & libraries",
                "level3": "TypeScript",
                "level1_id": "84dff39f-2ed6-408c-a188-14cf55a09c5b",
                "level2_id": "e13b48c1-fd0f-4ecc-89de-58e9312b9435",
                "level3_id": "fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"
            }
        ],
        "end_date": null,
        "position": "Senior Software Engineer + Team Lead",
        "start_date": "2019-10-01T00:00:00",
        "description": "Draper, Utah, United Statesn• Architected Expert Portal* from the ground up using a Node/Typescriptnbackend, anPostgreSQL database, a GraphQL API layer, a Webpack build process, with anTypescript/React front-end and XState for state managementn• Enforced coding best practices with linting rules and code formatters bynautomating it inngit workflown• Automated deployment Expert Portal* to EC2 instances and the @pluralsightnNPMnartifactory using Github, TeamCity, and Octopusn• Improved product team workflow by building a browser extention to addnLeanKit cardntemplate functionalityn• Consumed and published data through Kafka streams and RabbitMQnmessagesn• Interviewed, onboarded, and trained junior to mid-level engineers",
        "company_name": "Pluralsight"
    },
    {
        "id": "9e2c2b44-39a4-4369-b237-c51fd938e61d",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024",
                "level1": "Programming languages",
                "level2": "Scripting languages",
                "level3": "JavaScript",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "313384d2-486d-4b7f-ba33-76b1cd696f0a",
                "level3_id": "012abcd1-3a6a-4803-a47e-42f46b402024"
            }
        ],
        "end_date": "2019-10-01T00:00:00",
        "position": "Software Engineer II",
        "start_date": "2017-11-01T00:00:00",
        "description": "Greater Salt Lake City AreanWorked on the KSL Jobs Classifieds team as a full-stack developer. Followingnthe scrum methodology, I added new features and maintained all thingsnpowered by KSL Jobs.n• Built and deployed a "white label" version of KSL Jobs for the Silicon Slopesnbrand. (https://siliconslopes.ksl.com)n• Rewrote major sections of the current KSL Jobs site in React.jsn• Automated querying data for reports and analytic purposes through Node andnPHP scriptsn• Provided rich data tracking through Google Tag Manager, Google Analytics,nand BigQueryn• Migrated Solr search engine to ElasticSearch with a GraphQL API",
        "company_name": "Deseret Digital Media"
    },
    {
        "id": "efbf68f4-7bdc-4ab6-bba9-fbf7ec38aeef",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a",
                "level1": "Programming languages",
                "level2": "High-level languages",
                "level3": "PHP",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "f45abc59-1e7e-4853-a498-999fcf12d498",
                "level3_id": "4b266297-6e25-4443-90ec-248bded4225a"
            }
        ],
        "end_date": "2019-08-01T00:00:00",
        "position": "Full Stack Developer",
        "start_date": "2017-01-01T00:00:00",
        "description": "Provo, Utah AreanWorked with Appritech LLC to modernize their legacy software and add newnfeatures to automate their business processes.n•  Building new call handler from the ground up using JavaScript ES6,nBootstrap, SASS fornfront-end, and PHP7/Laravel for back-endn•  Implemented real-time call management system and built reporting API.nImproved call agentnproductivity by 70%n•  Upgraded deprecated PHP legacy code to PHP 7n•  Synchronized follow up calls with Twilio API functionality for SMS messagenforwardingn•  Installed and setup Apache server for an after hours call center",
        "company_name": "Appritech Software"
    },
    {
        "id": "2db60c6c-c214-4d9b-9034-baba676203a8",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a",
                "level1": "Programming languages",
                "level2": "High-level languages",
                "level3": "PHP",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "f45abc59-1e7e-4853-a498-999fcf12d498",
                "level3_id": "4b266297-6e25-4443-90ec-248bded4225a"
            }
        ],
        "end_date": "2017-09-01T00:00:00",
        "position": "Web Developer",
        "start_date": "2017-02-01T00:00:00",
        "description": "Provo, Utah AreanWorked with Redcore LLC to build entrepreneurial tools, marketing tools, andnadding new functionality to the current Wordpress-integrated website andnCMS.n• Built Brand Management website from the ground up using Bootstrap andnJavaScript on thenfront end, with PHP and MySQL on the back-endn• Automated managerial and accounting tasks, such as invoice generation,nand transactions using Stripe APIn• Debugged and perfected current web applications to enrich UXn• Doubled clientele by expanding Redcore services offered to include websitencreation andnmanagement",
        "company_name": "Redcore LLC"
    },
    {
        "id": "c3f1d5b2-5586-477d-ae4c-e2927463244e",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024",
                "level1": "Programming languages",
                "level2": "Scripting languages",
                "level3": "JavaScript",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "313384d2-486d-4b7f-ba33-76b1cd696f0a",
                "level3_id": "012abcd1-3a6a-4803-a47e-42f46b402024"
            }
        ],
        "end_date": "2017-03-01T00:00:00",
        "position": "Software and Web Developer",
        "start_date": "2016-12-01T00:00:00",
        "description": "Provo, Utah AreanThis internship was focused on building core website and software fornCentaurific using the LAMP stack.n• Created dynamic landing pages for products to generate leadsn• Designed a MySQL database to organize data and generate excel reportsn• Revitalized marketing efforts through analytics and a social media sharingnpage",
        "company_name": "Centaurific"
    }
]

我想查询该结构。我尝试了这个以及它的其他一些变体,但没有成功:

SELECT workexp
FROM foo,
jsonb_array_elements(work_experience) workexp;

我收到此错误消息:

ERROR:  function jsonb_array_elements_text(jsonb[]) does not exist
LINE 3: jsonb_array_elements(work_experience) workexp;
    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

我查找的所有内容都有有关遍历和查询 jsonb 行的文档,但没有 jsonb[]< /代码> 的。如何查询“skills”.“id”?

编辑——解决方案: 给出的所有解决方案都有帮助,但我选择了 Ramin Faracov 建议使用 unnest 。谢谢你!

这是我提出的查询:

SELECT
    id expert_id,
    we_sk->>'id' work_exp_skill_id
FROM (
    SELECT 
        id,
        unnest(work_experience)->'skills' we
    FROM foo_json
) sk,
jsonb_array_elements(sk.we) we_sk;

Having a hard time traversing and querying elements from a jsonb[] row.

CREATE TABLE foo (
  id uuid PRIMARY KEY,
  work_experience jsonb[] NOT NULL
);

INSERT INTO foo (id, work_experience)
VALUES (
  'b4e942a0-49b4-4fa7-8f7a-5fbf0541d1c9',
  E'{"{\\"id\\": \\"7cd74bae-ff5b-4f58-ab20-0218f820ffff\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"Scripting languages\\", \\"level3\\": \\"TypeScript\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"313384d2-486d-4b7f-ba33-76b1cd696f0a\\", \\"level3_id\\": \\"fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\"}, {\\"id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|686af7e4-6d58-4148-b227-3bf65ff10273\\", \\"level1\\": \\"Software frameworks & libraries\\", \\"level2\\": \\"Frontend frameworks & libraries\\", \\"level3\\": \\"React\\", \\"level1_id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b\\", \\"level2_id\\": \\"e13b48c1-fd0f-4ecc-89de-58e9312b9435\\", \\"level3_id\\": \\"686af7e4-6d58-4148-b227-3bf65ff10273\\"}, {\\"id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b|c4e54726-7bd5-44bb-8597-a05eb2272e2b|cda4441f-dba6-495c-9e2e-7429bd5e0465\\", \\"level1\\": \\"Software frameworks & libraries\\", \\"level2\\": \\"Backend frameworks & libraries\\", \\"level3\\": \\"Node.js\\", \\"level1_id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b\\", \\"level2_id\\": \\"c4e54726-7bd5-44bb-8597-a05eb2272e2b\\", \\"level3_id\\": \\"cda4441f-dba6-495c-9e2e-7429bd5e0465\\"}, {\\"id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\", \\"level1\\": \\"Software frameworks & libraries\\", \\"level2\\": \\"Frontend frameworks & libraries\\", \\"level3\\": \\"TypeScript\\", \\"level1_id\\": \\"84dff39f-2ed6-408c-a188-14cf55a09c5b\\", \\"level2_id\\": \\"e13b48c1-fd0f-4ecc-89de-58e9312b9435\\", \\"level3_id\\": \\"fd3c41b8-8c15-47e2-a80d-cf3683b2d0da\\"}], \\"end_date\\": null, \\"position\\": \\"Senior Software Engineer + Team Lead\\", \\"start_date\\": \\"2019-10-01T00:00:00\\", \\"description\\": \\"Draper, Utah, United States\\\\n• Architected Expert Portal* from the ground up using a Node/Typescript\\\\nbackend, a\\\\nPostgreSQL database, a GraphQL API layer, a Webpack build process, with a\\\\nTypescript/React front-end and XState for state management\\\\n• Enforced coding best practices with linting rules and code formatters by\\\\nautomating it in\\\\ngit workflow\\\\n• Automated deployment Expert Portal* to EC2 instances and the @pluralsight\\\\nNPM\\\\nartifactory using Github, TeamCity, and Octopus\\\\n• Improved product team workflow by building a browser extention to add\\\\nLeanKit card\\\\ntemplate functionality\\\\n• Consumed and published data through Kafka streams and RabbitMQ\\\\nmessages\\\\n• Interviewed, onboarded, and trained junior to mid-level engineers\\", \\"company_name\\": \\"Pluralsight\\"}","{\\"id\\": \\"9e2c2b44-39a4-4369-b237-c51fd938e61d\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"Scripting languages\\", \\"level3\\": \\"JavaScript\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"313384d2-486d-4b7f-ba33-76b1cd696f0a\\", \\"level3_id\\": \\"012abcd1-3a6a-4803-a47e-42f46b402024\\"}], \\"end_date\\": \\"2019-10-01T00:00:00\\", \\"position\\": \\"Software Engineer II\\", \\"start_date\\": \\"2017-11-01T00:00:00\\", \\"description\\": \\"Greater Salt Lake City Area\\\\nWorked on the KSL Jobs Classifieds team as a full-stack developer. Following\\\\nthe scrum methodology, I added new features and maintained all things\\\\npowered by KSL Jobs.\\\\n• Built and deployed a \\\\\\"white label\\\\\\" version of KSL Jobs for the Silicon Slopes\\\\nbrand. (https://siliconslopes.ksl.com)\\\\n• Rewrote major sections of the current KSL Jobs site in React.js\\\\n• Automated querying data for reports and analytic purposes through Node and\\\\nPHP scripts\\\\n• Provided rich data tracking through Google Tag Manager, Google Analytics,\\\\nand BigQuery\\\\n• Migrated Solr search engine to ElasticSearch with a GraphQL API\\", \\"company_name\\": \\"Deseret Digital Media\\"}","{\\"id\\": \\"efbf68f4-7bdc-4ab6-bba9-fbf7ec38aeef\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"High-level languages\\", \\"level3\\": \\"PHP\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"f45abc59-1e7e-4853-a498-999fcf12d498\\", \\"level3_id\\": \\"4b266297-6e25-4443-90ec-248bded4225a\\"}], \\"end_date\\": \\"2019-08-01T00:00:00\\", \\"position\\": \\"Full Stack Developer\\", \\"start_date\\": \\"2017-01-01T00:00:00\\", \\"description\\": \\"Provo, Utah Area\\\\nWorked with Appritech LLC to modernize their legacy software and add new\\\\nfeatures to automate their business processes.\\\\n•  Building new call handler from the ground up using JavaScript ES6,\\\\nBootstrap, SASS for\\\\nfront-end, and PHP7/Laravel for back-end\\\\n•  Implemented real-time call management system and built reporting API.\\\\nImproved call agent\\\\nproductivity by 70%\\\\n•  Upgraded deprecated PHP legacy code to PHP 7\\\\n•  Synchronized follow up calls with Twilio API functionality for SMS message\\\\nforwarding\\\\n•  Installed and setup Apache server for an after hours call center\\", \\"company_name\\": \\"Appritech Software\\"}","{\\"id\\": \\"2db60c6c-c214-4d9b-9034-baba676203a8\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"High-level languages\\", \\"level3\\": \\"PHP\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"f45abc59-1e7e-4853-a498-999fcf12d498\\", \\"level3_id\\": \\"4b266297-6e25-4443-90ec-248bded4225a\\"}], \\"end_date\\": \\"2017-09-01T00:00:00\\", \\"position\\": \\"Web Developer\\", \\"start_date\\": \\"2017-02-01T00:00:00\\", \\"description\\": \\"Provo, Utah Area\\\\nWorked with Redcore LLC to build entrepreneurial tools, marketing tools, and\\\\nadding new functionality to the current Wordpress-integrated website and\\\\nCMS.\\\\n• Built Brand Management website from the ground up using Bootstrap and\\\\nJavaScript on the\\\\nfront end, with PHP and MySQL on the back-end\\\\n• Automated managerial and accounting tasks, such as invoice generation,\\\\nand transactions using Stripe API\\\\n• Debugged and perfected current web applications to enrich UX\\\\n• Doubled clientele by expanding Redcore services offered to include website\\\\ncreation and\\\\nmanagement\\", \\"company_name\\": \\"Redcore LLC\\"}","{\\"id\\": \\"c3f1d5b2-5586-477d-ae4c-e2927463244e\\", \\"skills\\": [{\\"id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024\\", \\"level1\\": \\"Programming languages\\", \\"level2\\": \\"Scripting languages\\", \\"level3\\": \\"JavaScript\\", \\"level1_id\\": \\"c8a5328d-87ba-419d-802f-80b1d940bb68\\", \\"level2_id\\": \\"313384d2-486d-4b7f-ba33-76b1cd696f0a\\", \\"level3_id\\": \\"012abcd1-3a6a-4803-a47e-42f46b402024\\"}], \\"end_date\\": \\"2017-03-01T00:00:00\\", \\"position\\": \\"Software and Web Developer\\", \\"start_date\\": \\"2016-12-01T00:00:00\\", \\"description\\": \\"Provo, Utah Area\\\\nThis internship was focused on building core website and software for\\\\nCentaurific using the LAMP stack.\\\\n• Created dynamic landing pages for products to generate leads\\\\n• Designed a MySQL database to organize data and generate excel reports\\\\n• Revitalized marketing efforts through analytics and a social media sharing\\\\npage\\", \\"company_name\\": \\"Centaurific\\"}"}'
);

(Here is what that work_experience row looks like in JSON:)

[
    {
        "id": "7cd74bae-ff5b-4f58-ab20-0218f820ffff",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da",
                "level1": "Programming languages",
                "level2": "Scripting languages",
                "level3": "TypeScript",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "313384d2-486d-4b7f-ba33-76b1cd696f0a",
                "level3_id": "fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"
            },
            {
                "id": "84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|686af7e4-6d58-4148-b227-3bf65ff10273",
                "level1": "Software frameworks & libraries",
                "level2": "Frontend frameworks & libraries",
                "level3": "React",
                "level1_id": "84dff39f-2ed6-408c-a188-14cf55a09c5b",
                "level2_id": "e13b48c1-fd0f-4ecc-89de-58e9312b9435",
                "level3_id": "686af7e4-6d58-4148-b227-3bf65ff10273"
            },
            {
                "id": "84dff39f-2ed6-408c-a188-14cf55a09c5b|c4e54726-7bd5-44bb-8597-a05eb2272e2b|cda4441f-dba6-495c-9e2e-7429bd5e0465",
                "level1": "Software frameworks & libraries",
                "level2": "Backend frameworks & libraries",
                "level3": "Node.js",
                "level1_id": "84dff39f-2ed6-408c-a188-14cf55a09c5b",
                "level2_id": "c4e54726-7bd5-44bb-8597-a05eb2272e2b",
                "level3_id": "cda4441f-dba6-495c-9e2e-7429bd5e0465"
            },
            {
                "id": "84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da",
                "level1": "Software frameworks & libraries",
                "level2": "Frontend frameworks & libraries",
                "level3": "TypeScript",
                "level1_id": "84dff39f-2ed6-408c-a188-14cf55a09c5b",
                "level2_id": "e13b48c1-fd0f-4ecc-89de-58e9312b9435",
                "level3_id": "fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"
            }
        ],
        "end_date": null,
        "position": "Senior Software Engineer + Team Lead",
        "start_date": "2019-10-01T00:00:00",
        "description": "Draper, Utah, United Statesn• Architected Expert Portal* from the ground up using a Node/Typescriptnbackend, anPostgreSQL database, a GraphQL API layer, a Webpack build process, with anTypescript/React front-end and XState for state managementn• Enforced coding best practices with linting rules and code formatters bynautomating it inngit workflown• Automated deployment Expert Portal* to EC2 instances and the @pluralsightnNPMnartifactory using Github, TeamCity, and Octopusn• Improved product team workflow by building a browser extention to addnLeanKit cardntemplate functionalityn• Consumed and published data through Kafka streams and RabbitMQnmessagesn• Interviewed, onboarded, and trained junior to mid-level engineers",
        "company_name": "Pluralsight"
    },
    {
        "id": "9e2c2b44-39a4-4369-b237-c51fd938e61d",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024",
                "level1": "Programming languages",
                "level2": "Scripting languages",
                "level3": "JavaScript",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "313384d2-486d-4b7f-ba33-76b1cd696f0a",
                "level3_id": "012abcd1-3a6a-4803-a47e-42f46b402024"
            }
        ],
        "end_date": "2019-10-01T00:00:00",
        "position": "Software Engineer II",
        "start_date": "2017-11-01T00:00:00",
        "description": "Greater Salt Lake City AreanWorked on the KSL Jobs Classifieds team as a full-stack developer. Followingnthe scrum methodology, I added new features and maintained all thingsnpowered by KSL Jobs.n• Built and deployed a "white label" version of KSL Jobs for the Silicon Slopesnbrand. (https://siliconslopes.ksl.com)n• Rewrote major sections of the current KSL Jobs site in React.jsn• Automated querying data for reports and analytic purposes through Node andnPHP scriptsn• Provided rich data tracking through Google Tag Manager, Google Analytics,nand BigQueryn• Migrated Solr search engine to ElasticSearch with a GraphQL API",
        "company_name": "Deseret Digital Media"
    },
    {
        "id": "efbf68f4-7bdc-4ab6-bba9-fbf7ec38aeef",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a",
                "level1": "Programming languages",
                "level2": "High-level languages",
                "level3": "PHP",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "f45abc59-1e7e-4853-a498-999fcf12d498",
                "level3_id": "4b266297-6e25-4443-90ec-248bded4225a"
            }
        ],
        "end_date": "2019-08-01T00:00:00",
        "position": "Full Stack Developer",
        "start_date": "2017-01-01T00:00:00",
        "description": "Provo, Utah AreanWorked with Appritech LLC to modernize their legacy software and add newnfeatures to automate their business processes.n•  Building new call handler from the ground up using JavaScript ES6,nBootstrap, SASS fornfront-end, and PHP7/Laravel for back-endn•  Implemented real-time call management system and built reporting API.nImproved call agentnproductivity by 70%n•  Upgraded deprecated PHP legacy code to PHP 7n•  Synchronized follow up calls with Twilio API functionality for SMS messagenforwardingn•  Installed and setup Apache server for an after hours call center",
        "company_name": "Appritech Software"
    },
    {
        "id": "2db60c6c-c214-4d9b-9034-baba676203a8",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|f45abc59-1e7e-4853-a498-999fcf12d498|4b266297-6e25-4443-90ec-248bded4225a",
                "level1": "Programming languages",
                "level2": "High-level languages",
                "level3": "PHP",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "f45abc59-1e7e-4853-a498-999fcf12d498",
                "level3_id": "4b266297-6e25-4443-90ec-248bded4225a"
            }
        ],
        "end_date": "2017-09-01T00:00:00",
        "position": "Web Developer",
        "start_date": "2017-02-01T00:00:00",
        "description": "Provo, Utah AreanWorked with Redcore LLC to build entrepreneurial tools, marketing tools, andnadding new functionality to the current Wordpress-integrated website andnCMS.n• Built Brand Management website from the ground up using Bootstrap andnJavaScript on thenfront end, with PHP and MySQL on the back-endn• Automated managerial and accounting tasks, such as invoice generation,nand transactions using Stripe APIn• Debugged and perfected current web applications to enrich UXn• Doubled clientele by expanding Redcore services offered to include websitencreation andnmanagement",
        "company_name": "Redcore LLC"
    },
    {
        "id": "c3f1d5b2-5586-477d-ae4c-e2927463244e",
        "skills": [
            {
                "id": "c8a5328d-87ba-419d-802f-80b1d940bb68|313384d2-486d-4b7f-ba33-76b1cd696f0a|012abcd1-3a6a-4803-a47e-42f46b402024",
                "level1": "Programming languages",
                "level2": "Scripting languages",
                "level3": "JavaScript",
                "level1_id": "c8a5328d-87ba-419d-802f-80b1d940bb68",
                "level2_id": "313384d2-486d-4b7f-ba33-76b1cd696f0a",
                "level3_id": "012abcd1-3a6a-4803-a47e-42f46b402024"
            }
        ],
        "end_date": "2017-03-01T00:00:00",
        "position": "Software and Web Developer",
        "start_date": "2016-12-01T00:00:00",
        "description": "Provo, Utah AreanThis internship was focused on building core website and software fornCentaurific using the LAMP stack.n• Created dynamic landing pages for products to generate leadsn• Designed a MySQL database to organize data and generate excel reportsn• Revitalized marketing efforts through analytics and a social media sharingnpage",
        "company_name": "Centaurific"
    }
]

I'd like to query out the structure. I tried this and a few other variants of it to no avail:

SELECT workexp
FROM foo,
jsonb_array_elements(work_experience) workexp;

And I get this error message:

ERROR:  function jsonb_array_elements_text(jsonb[]) does not exist
LINE 3: jsonb_array_elements(work_experience) workexp;
    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

Everything I look up has documentation on traversing and querying jsonb rows, but not jsonb[] ones. How can I query for the "skills"."id"s?

EDIT -- SOLUTION:
All of the solutions given were helpful but I selected Ramin Faracov for suggesting to use unnest. Thank you!

This is the query that I came up with:

SELECT
    id expert_id,
    we_sk->>'id' work_exp_skill_id
FROM (
    SELECT 
        id,
        unnest(work_experience)->'skills' we
    FROM foo_json
) sk,
jsonb_array_elements(sk.we) we_sk;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2025-01-27 06:20:25

在 PostgreSQL 中,所有 jsonb 数组函数 仅用于使用 JSONB 类型,而不是 JSONB[]

  1. 第一种方式可以使用 unnest 函数将数组转换为行,然后可以使用 jsonb_array_elements

  2. 第二种方式将表中的类型 JSONB[] 更改为 JSON。不用担心,您仍然可以将上面写入的 JSON 字符串数据插入到 JSONB 字段中,不会出现任何问题。在 JSONJSONB 字符串中,我们可以使用格式化字符串的数组。 Jsonb 数组函数 仅适用于在 JSON 字符串内写入的数组。

In PostgreSQL all jsonb array functions only for using JSONB types, not JSONB[].

  1. First-way you can use unnest function to convert arrays to rows, after then you can use jsonb_array_elements.

  2. Second-way change type JSONB[] to JSON in your table. Don't worry, you can still insert your JSON string data which is written above into JSONB field without any problems. Inside the JSON and JSONB strings, we can use arrays using formatted strings. Jsonb array functions only for arrays written inside JSON string.

一个人的旅程 2025-01-27 06:20:25

db fiddle 演示

  • 您不需要 jsonb[],只需使用 jsonb 即可。
  • 您需要正确地重新格式化 jsonb,以便可以插入到 postgresql。
    我使用 datagrip 语法突出显示将代码重新格式化为数据库。就像这个
  • CTE更容易重新格式化逻辑。所以我用它。

--query 过滤 mainid 然后是内部 id。
-- "84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"

with a as (
SELECT workexp
FROM foo_json,
jsonb_array_elements(work_experience) workexp),
b as(select workexp['id'] mainid,
            jsonb_array_elements(workexp['skills']) last_level,
            count(*) over() from  a),
c as(select
         last_level,
    pg_typeof(mainid) from b
    where mainid::text = '"7cd74bae-ff5b-4f58-ab20-0218f820ffff"'::text)
select *,
       last_level['level1'],
       last_level['level2'],
       last_level['level3'],
       last_level['level1_id'],
       last_level['level2_id'],
       last_level['level3_id']
from c
where
    last_level['id']::text
= '"84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"'::text;

--基于 mainid 的查询过滤器(不是内部嵌套)

with a as (
SELECT workexp
FROM foo_json,
jsonb_array_elements(work_experience) workexp),
b as(select workexp['id'] mainid, workexp['skills'], count(*) over() from  a)
select *, pg_typeof(mainid) from b
where mainid::text = '"7cd74bae-ff5b-4f58-ab20-0218f820ffff"'::text;

postgresql 15(开发)
在postgresql 15中,有json_table。使嵌套 json/jsonb 查询更加容易。

SELECT jt.* FROM foo_json,
JSON_TABLE(work_experience, '$[*]' COLUMNS(
main_id text path '$.id', start_date date path '$.start_date',
position text path '$.position', company_name text path '$.company_name',
NESTED PATH '$.skills[*]' COLUMNS (
        innerid text path '$.id',
      level2 text, level3 text)
)) jt;

db fiddle demo.

  • you don't need jsonb[], just use jsonb.
  • You need to properly reformat the jsonb, so that you can insert to postgresql.
    I user datagrip syntax highlight to reformat your code to db. like this.
  • CTE is more easier to reformat the logic. So i use it.

--query filter mainid then the inner id.
-- "84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"

with a as (
SELECT workexp
FROM foo_json,
jsonb_array_elements(work_experience) workexp),
b as(select workexp['id'] mainid,
            jsonb_array_elements(workexp['skills']) last_level,
            count(*) over() from  a),
c as(select
         last_level,
    pg_typeof(mainid) from b
    where mainid::text = '"7cd74bae-ff5b-4f58-ab20-0218f820ffff"'::text)
select *,
       last_level['level1'],
       last_level['level2'],
       last_level['level3'],
       last_level['level1_id'],
       last_level['level2_id'],
       last_level['level3_id']
from c
where
    last_level['id']::text
= '"84dff39f-2ed6-408c-a188-14cf55a09c5b|e13b48c1-fd0f-4ecc-89de-58e9312b9435|fd3c41b8-8c15-47e2-a80d-cf3683b2d0da"'::text;

--query filter based on mainid (not the inner nested)

with a as (
SELECT workexp
FROM foo_json,
jsonb_array_elements(work_experience) workexp),
b as(select workexp['id'] mainid, workexp['skills'], count(*) over() from  a)
select *, pg_typeof(mainid) from b
where mainid::text = '"7cd74bae-ff5b-4f58-ab20-0218f820ffff"'::text;

postgresql 15 (dev)
In postgresql 15, there is json_table. make nested json/jsonb query more easier.

SELECT jt.* FROM foo_json,
JSON_TABLE(work_experience, '$[*]' COLUMNS(
main_id text path '$.id', start_date date path '$.start_date',
position text path '$.position', company_name text path '$.company_name',
NESTED PATH '$.skills[*]' COLUMNS (
        innerid text path '$.id',
      level2 text, level3 text)
)) jt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文