Postgresql, retrieve value for specific key from json array

后端 未结 1 1857
礼貌的吻别
礼貌的吻别 2021-01-12 12:09

I have Postgres JSONB array of objects, looking like this :

\'[
  {
    \"skillId\": \"1\",
    \"skillLevel\": 42
  },
  {
    \"skillId\": \"2\",
    \"sk         


        
1条回答
  •  被撕碎了的回忆
    2021-01-12 12:42

    In Postgres 9.4+ use the function jsonb_array_elements() in a lateral join:

    select (elem->>'skillLevel')::int as skill_level
    from my_table
    cross join jsonb_array_elements(json_col) elem
    where elem->>'skillId' = '1';
    

    You can implement the idea in a simple function, e.g:

    create or replace function extract_skill_level(json_data jsonb, id int)
    returns integer language sql as $$
        select (elem->>'skillLevel')::int
        from jsonb_array_elements(json_data) elem
        where elem->>'skillId' = id::text
    $$;
    
    select extract_skill_level(json_col, 1) as skill_level
    from my_table;
    

    In Postgres 12+ you have a nice alternative in the form of jsonb path functions:

    select (
        jsonb_path_query(
            json_col, 
            '$[*] ? (@.skillId == "1")'
            )->'skillLevel'
        )::int as skill_level
    from my_table;
    

    Db<>Fiddle.

    Read more about JSON Functions and Operators.

    0 讨论(0)
提交回复
热议问题