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.

1
  • 1
    raw_json should be defined as jsonb or at least json, not as text Commented May 25, 2022 at 6:04

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.