使用JSON_ARRAY_ELEMENTS扩展JSON数组时,是否可以将索引值添加到PostgreSQL查询的行中?

发布于 2025-02-01 11:38:43 字数 765 浏览 2 评论 0原文

我有一个以JSON格式的简历数据数据库,我正在尝试转换。 每个jsib中的一个部分是work_history,这是json数组的形式,即

"work_experience":[
   {
      "job_title":"title",
      "job_description":"description"
   },
   {
      "job_title":"title",
      "job_description":"description"
   }
]

我在每个简历(JSON文件)上迭代并使用dbt和postgresql将这些数据导入到新表中,而数组的每个元素都是一个元素与简历的相关元数据的新行。这是我为此使用的代码,

select
    json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience')  as we,
    json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience') -> 'job_title' as "name",
    rjt.uuid as uuid
from raw_json_table rjt

我需要做的最后一件事是添加一列列出了每个作业来自其单个workexperience阵列中的索引,即如果作业是数组中的第三个元素,它将具有2在“ source_location”列中。我如何生成此索引,以使每个新的JSON文件始于0。

I have a database of resume data in json format which I am trying to transform.
One of the sections in each jsib is work_history, this is in the form of a json array i.e.

"work_experience":[
   {
      "job_title":"title",
      "job_description":"description"
   },
   {
      "job_title":"title",
      "job_description":"description"
   }
]

I am iterating over each resume (json file) and importing this data into a new table using dbt and postgreSQL with each element of the array being a new row with the associated metadata of the resume. Here is the code I used for this

select
    json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience')  as we,
    json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience') -> 'job_title' as "name",
    rjt.uuid as uuid
from raw_json_table rjt

The last thing that I need to do is add a column that lists the index that each job came from within its individual workexperience array i.e. if a job was the third element in the array it would have a 2 in the "source_location" column. How can I generate this index such that it starts at 0 for each new json file.

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

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

发布评论

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

评论(1

回首观望 2025-02-08 11:38:43

将函数移至从子句中(应使用集合返回函数)。然后,您可以将与法令一起返回一列,该列指示数组内的索引

select w.experience as we,
       w.experience ->> 'job_title' as "name",
       w.experience ->> 'job_description' as "description",
       w.idx as "index",
       rjt.uuid as uuid
from raw_json_table rjt
  left join json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience') with ordinality 
            as w(experience, idx) on true

左JOIN是必要的,以便从RAW_JSON_SSON_TABLE行回行仍然不包含数组元素。

Move the function to the FROM clause (where set-returning functions should be used). Then you can use with ordinality which also returns a column that indicates the index inside the array

select w.experience as we,
       w.experience ->> 'job_title' as "name",
       w.experience ->> 'job_description' as "description",
       w.idx as "index",
       rjt.uuid as uuid
from raw_json_table rjt
  left join json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience') with ordinality 
            as w(experience, idx) on true

The left join is necessary so that rows from raw_json_table that don't contain array elements are still included.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文