简体   繁体   English

尝试构造 PostgreSQL 查询以从对象、数组、对象、数组、对象中的 JSON 中提取文本值

[英]Trying to construct PostgreSQL Query to extract from JSON a text value in an object, in an array, in an object, in an array, in an object

I am constructing an interface between a PostgreSQL system and a SQL Server system and am attempting to "flatten" the structure of the JSON data to facilitate this.我正在构建 PostgreSQL 系统和 SQL Server 系统之间的接口,并试图“扁平化”JSON 数据的结构以促进这一点。 I'm very experienced in SQL Server but I'm new to both PostgreSQL and JSON.我在 SQL Server 方面非常有经验,但我对 PostgreSQL 和 JSON 都是新手。

The JSON contains essentially two types of structure: those of type "text" or "textarea" where the value I want is in an object named value (the first two cases below) and those of type "select" where the value object points to an id object in a lower-level options array (the third case below). JSON 本质上包含两种类型的结构: type为“text”或“textarea”的结构,其中我想要的值位于名为value的对象中(下面的前两种情况),以及type “select”的结构,其中value对象指向较低级别options数组中的id对象(下面的第三种情况)。

{
    "baseGroupId": {
        "fields": [
            {
                "id": "1f53",
                "name": "Location",
                "type": "text",
                "options": [],
                "value": "Over the rainbow"
            },
            {
                "id": "b547",
                "name": "Description",
                "type": "textarea",
                "options": [],
                "value": "A place of wonderful discovery"
            },
            {
                "id": "c12f",
                "name": "Assessment",
                "type": "select",
                "options": [
                    {
                        "id": "e5fd",
                        "name": "0"
                    },
                    {
                        "id": "e970",
                        "name": "1"
                    },
                    {
                        "id": "0ff4",
                        "name": "2"
                    },
                    {
                        "id": "2db3",
                        "name": "3"
                    },
                    {
                        "id": "241f",
                        "name": "4"
                    },
                    {
                        "id": "3f52",
                        "name": "5"
                    }
                ],
                "value": "241f"
            }
        ]
    }
}

Those with a sharp eye will see that the value of the last value object "241f" can also be seen within the options array against one of the id objects.眼尖的人会发现,最后一个value对象“241f”的值也可以在options数组中针对其中一个id对象看到。 When nested like this I need to extract the value of the corresponding name , in this case "4".当像这样嵌套时,我需要提取相应name的值,在本例中为“4”。

The JSON-formatted information is in table customfield field textvalue . JSON 格式的信息位于表customfield字段textvalue中。 It's datatype is text but I'm coercing it to json .它的数据类型是文本,但我将其强制转换为json I was originally getting array set errors when trying to apply the criteria in a WHERE clause and then I read about using a LATERAL subquery instead.我最初在尝试在 WHERE 子句中应用条件时收到数组集错误,然后我阅读了有关使用 LATERAL 子查询的信息。 It now runs but returns all the options, not just the one matching the value .它现在运行但返回所有选项,而不仅仅是与value匹配的选项。

I'm afraid I couldn't get an SQL Fiddle working to reproduce my results, but I would really appreciate an examination of my query to see if the problem can be spotted.恐怕我无法让 SQL Fiddle 重现我的结果,但我非常感谢检查我的查询以查看是否可以发现问题。

with cte_custombundledfields as
     (
            select
                   textvalue
                 , cfname
                 , json_array_elements(textvalue::json -> 'baseGroupId'->'fields') ->> 'name'  as name
                 , json_array_elements(textvalue::json -> 'baseGroupId'->'fields') ->> 'value' as value
                 , json_array_elements(textvalue::json -> 'baseGroupId'->'fields') ->> 'type'  as type
            from
                   customfield
     )
   , cte_custombundledfieldsoptions as
     (
            select *
                 , json_array_elements(json_array_elements(textvalue::json -> 'baseGroupId'->'fields') -> 'options') ->> 'name' as value2
            from
                   cte_custombundledfields                                                   x
                 , LATERAL json_array_elements(x.textvalue::json -> 'baseGroupId'->'fields') y
                 , LATERAL json_array_elements(y -> 'options')                               z
            where
                   type    = 'select'
                   and z ->> 'id' = x.value
     )
select *
from
       cte_custombundledfieldsoptions

I posted a much-simplified rewrite of this question which was answered by Bergi .我发布了一个由Bergi回答的这个问题的简化重写。

How do I query a string from JSON based on another string within the JSON in PostgreSQL? 如何根据 PostgreSQL 中 JSON 中的另一个字符串从 JSON 中查询一个字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM