0

I'm trying to parse a JSON column and map it into individual columns based on key-value pairs. Here's what my input would look like, and I've added the sample output. I am doing this in GCP Bigquery.

Input: JSON column

{"id":"1","timestamp":"2022-09-05", "data":{"fruits":"apple", "name":"abc"}},
{"id":"2","timestamp":"2022-09-06", "data":{"vegetables":"tomato", "name":"def"}},
{"id":"3","timestamp":"2022-09-07", "data":{"fruits":"banana", "name":"ghi"}}

Sample Output:

id  timestamp   fruits  vegetables  name
1   2022-09-05  apple   null        abc
2   2022-09-06  null    tomato      def
3   2022-09-07  banana  null        ghi

P.S. -> I've tried going through a few of the answers on similar use cases, but it didn't quite work for me.

Thanks in advance!

7
  • You want loading a Json file to a Bigquery table ? Commented Sep 6, 2022 at 21:32
  • Just to confirm, your JSON column is of STRING data type? Commented Sep 6, 2022 at 22:04
  • 2
    Your JSON is not valid JSON. 1) JSON does not use single quotes. 2) All keys must have a value. This is invalid: 'fruits':{'apple'} 3) Use a JSON validator to see the other errors. Commented Sep 7, 2022 at 2:12
  • What's your current error? What are the next step? Commented Sep 7, 2022 at 9:01
  • @MazlumTosun Yes, I want to load it to a BigQuery table. I would be reading the JSON from a table and then want to parse this json and load it as columns in the final table. Commented Sep 7, 2022 at 18:03

1 Answer 1

2

to parse a JSON column and map it into individual columns based on key-value pairs

Consider below

select 
  json_value(json, '$.id') id,
  json_value(json, '$.timestamp') timestamp,
  json_value(json, '$.data.fruits') fruits,
  json_value(json, '$.data.vegetables') vegetables,
  json_value(json, '$.data.name') name
from your_table          

if applied to sample data in your question - output is

enter image description here

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

4 Comments

Mikhail - This is what I'm using right now. However, my json won't have the said keys every time and may change. So, I was looking for a dynamic solution that could break the keys into column headers & its values as records.
check out stackoverflow.com/a/73505124/5221944 as an example of how to make it dynamic. there are plenty more posts with different variations of that technique answered by myself alone, not to mention many more answered by others. Hope this will help :o)
This works! Thank you so much, Mikhail. Just curious, I see that the bytes shuffled and slot time consumed is quite high, even for a small table. Is there a more inexpensive way to achieve this?
this is the easiest and least expensive option i know of :o)

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.