提取物?行中的JSON数组对象数

时间:2018-06-20 13:08:02

标签: postgresql metabase

因此,我尝试将一些大型Json数组逐行转换为格式(postgres db)。想象一下

{“searchResults”:[{“id”:“89705”,“score”:42,“title”:“Foo.”,“properties”:{…

目前,我们在数组中获得的最多结果约为300个id,并明确表示;

Data::Json->'searchResults'->0->'tags'->0->>'label' as "Tag - Result 1",
...
Data::Json->'searchResults'->300->'tags'->0->>'label' as "Tag - Result 

1“,

理想输出

Array, ID , Score, Title
----
1 89705, 42, foo
1 89706, 34, bar
2 90003, 54, thing
2 98594, 53, that

(所以1,2代表初始表中的不同行,它们都包含??? JSON数据数组的对象数量)

扩展的JSON

    {
"searchResults": [
{
  "id": "897096",
  "score": 42,
  "title": "foo.",
  "properties": {
    "@type": "blah",
  },
  "publishedDate": "2018-06-30T10:20:20.555040Z",
  "comments": [
    {
      "content": "",
      "owner": {
        "firstName": "",
        "id": 0,
        "lastName": ""
      },
      "id": 0,
      "contentType": "",
      "documentPk": 0,
      "workflowStep": 0,
      "order": 0
    }
  ],
  "tags": [
    {
      "tag": 783,
      "label": "NO",
      "iconClass": "",
      "subGroup": "",
      "exclude": false
    },
    {
      "tag": 786,
      "label": "Different name",
      "iconClass": "",
      "subGroup": "",
      "exclude": false
    }
  ],
  "reviewTags": [
    {
      "tag": 2,
      "label": "Accept",
      "iconClass": "",
      "subGroup": "",
      "exclude": false
    }
  ],
  "original": {
    ..."names": [
      {
        "full_name": "This name"
      }
    ],
    "Entry Type": "Organization",
    "Last Updated": "2018/05/03",
    "Hit Category": "N/A",
    "Aliases": [
      "Olaj",
      "hbhbhb"
    ]
  },
  "snippet": "",
  "url": "",
  "source": "_"
},
{
  "id": "879057",
  "score": 36,
  "title": "name of company",
  "properties": {
    "@type": "",
    "category": "SOE",
    "type": "Organization",
    "country": "Korea, Republic Of",
    "subcategory": ""
  },
  "publishedDate": "2018-05-31T10:20:20.559714Z",
  "comments": [
    {
      "content": "",
      "owner": {
        "firstName": "",
        "id": 0,
        "lastName": ""
      },
      "id": 0,
      "contentType": "",
      "documentPk": 0,
      "workflowStep": 0,
      "order": 0
    }
  ],
  "tags": [
    {
      "tag": 783,
      "label": "NO",
      "iconClass": "",
      "subGroup": "",
      "exclude": false
    },
    {
      "tag": 786,
      "label": "Different name",
      "iconClass": "",
      "subGroup": "",
      "exclude": false
    }

有人建议我在这里有什么选择吗?

感谢@a_horse_with_no_name,它非常有效。

1 个答案:

答案 0 :(得分:0)

目前尚不清楚JSON如何在数组中继续,但似乎您正在寻找类似的内容:

with testdata(props) as (
 values (
  '{"searchResults": 
     [ 
       {"id":"89705","score":42,"title":"Foo"},
       {"id":"89706","score":34, "title":"bar"}
     ]}'::jsonb
 ) 
)
select x.idx, 
       x.val ->> 'id' as id,
       x.val ->> 'score' as score, 
       x.val ->> 'title' as title
from testdata, jsonb_array_elements(props -> 'searchResults') with ordinality as x (val, idx);

返回

idx | id    | score | title
----+-------+-------+------
  1 | 89705 | 42    | Foo  
  2 | 89706 | 34    | bar