使用JSON_ARRAY_ELEMENTS扩展JSON数组时,是否可以将索引值添加到PostgreSQL查询的行中?
我有一个以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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将函数移至从子句中(应使用集合返回函数)。然后,您可以将
与法令
一起返回一列,该列指示数组内的索引左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 arrayThe
left join
is necessary so that rows fromraw_json_table
that don't contain array elements are still included.