kalyan4uonly
kalyan4uonly

Reputation: 335

how to extract a value from json string

I have a source table as like below

enter image description here

on top this table there is a business logic implemented as like below

select id, TO_JSON_STRING(ARRAY_AGG(STRUCT(name,salary,deptno))) as d_list 
from `project.dataset.sample_tab` 
group by 1

Now after applying business logic the table look like below

enter image description here

From the above table i want to extract only salary column data for additional calculations. how we can extract ? please suggest me

I tried JSON_EXTRACT(d_list,"$.salary") but it is giving me null values

Upvotes: 0

Views: 45

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 173191

Use below

select id, json_extract(trim(d_list, '[]'),"$.salary") salary
from (
  select id, TO_JSON_STRING(ARRAY_AGG(STRUCT(name,salary,deptno))) as d_list 
  from `project.dataset.sample_tab` 
  group by 1
)             

if applied to sample data in your question - output is

enter image description here

Upvotes: 1

Related Questions