简体   繁体   中英

querying/parsing nested JSON in snowflake

I have a table that stores files like the following in each row and I want to be able to parse/flatten this file - the problem is that for the nested files ( history, dealer, vehicle) I am receiving NULL when I am using lateral flatten comment. Can someone help me to understand how I should properly parse this file?

{
  "auction_ends_at": "2020-10-22T15:32:55.155330",
  "bid_history": [],
  "created_at": "2020-10-22T11:55:03.154852",
  "dealer": {},
  "external_id": "PE28537",
  "highest_bidder": null,
  "history": [
    "d4c48842-07ef-418c-94db-971e0625e9e3",
    "7cf6f349-09e6-420d-ab8f-9f94395e87b6",
    "325f2283-527f-4101-8366-baface5f0009",
    "6bae782c-3bdd-4d58-9839-f4a9d4e25e49",
    "e7358eed-0c0e-412c-a9cd-52036fcd7893"
  ],
  "meta": null,
  "price": 8500,
  "price_currency": "EURO",
  "requires_scheduling": true,
  "reserve_price": 9137,
  "start_price": null,
  "transport_distance": null,
  "transport_fee": null,
  "updated_at": "2020-10-22T11:55:03.155402",
  "url": "https://www.XXXX.com/en/app/merchant/car/PE28537",
  "vehicle": {
    "additional_equipment": null,
    "additional_info": null,
    "airbags": null,
    "availability": null,
    "body": null,
    "body_condition": null,
    "category": null,
    "climatisation": null,
    "co2_emission_g_km": null,
    "color": "green",
    "color_manufacturer": null,
    "construction_year": null,
    "created_at": "2020-10-22T11:55:03.155249",
    "cubic_capacity": null,
    "current_location": "DE, Kitzingen",
    "defects": null,
    "emission_class": null,
    "emission_sticker": null,
    "equipment": null,
    "external_id": null,
    "extras": null,
    "first_registration": "07/2017",
    "fuel_consumption_l_100km": null,
    "fuel_type": "diesel",
    "horse_power": 105,
    "id": null,
    "interior_design": null,
    "label": "Fabia 1.4 TDI Ambition",
    "last_inspection": null,
    "make": "Skoda",
    "meta": null,
    "mileage_in_km": 58128,
    "model": "Fabia",
    "num_car_seats": null,
    "num_doors": null,
    "num_owners": null,
    "origin": null,
    "parking_sensors": null,
    "pdf_details_url": null,
    "price": null,
    "price_currency": null,
    "transmission": "manual",
    "updated_at": "2020-10-22T11:55:03.155530",
    "url": null,
    "vin": null
  }
}

I wrote a query list this:

select v.* from json_data,
lateral flatten( input=>j) v;

As I see, the dealer has no records, so it's ok to get a NULL. For history, I get the following array:

[
  "d4c48842-07ef-418c-94db-971e0625e9e3",
  "7cf6f349-09e6-420d-ab8f-9f94395e87b6",
  "325f2283-527f-4101-8366-baface5f0009",
  "6bae782c-3bdd-4d58-9839-f4a9d4e25e49",
  "e7358eed-0c0e-412c-a9cd-52036fcd7893"
]

And the vehicle returns an object:

{
  "additional_equipment": null,
  "additional_info": null,
  "airbags": null,
  "availability": null,
  "body": null,
  "body_condition": null,
  "category": null,
  "climatisation": null,
  "co2_emission_g_km": null,
  "color": "green",
  "color_manufacturer": null,
  "construction_year": null,
  "created_at": "2020-10-22T11:55:03.155249",
  "cubic_capacity": null,
  "current_location": "DE, Kitzingen",
  "defects": null,
  "emission_class": null,
  "emission_sticker": null,
  "equipment": null,
  "external_id": null,
  "extras": null,
  "first_registration": "07/2017",
  "fuel_consumption_l_100km": null,
  "fuel_type": "diesel",
  "horse_power": 105,
  "id": null,
  "interior_design": null,
  "label": "Fabia 1.4 TDI Ambition",
  "last_inspection": null,
  "make": "Skoda",
  "meta": null,
  "mileage_in_km": 58128,
  "model": "Fabia",
  "num_car_seats": null,
  "num_doors": null,
  "num_owners": null,
  "origin": null,
  "parking_sensors": null,
  "pdf_details_url": null,
  "price": null,
  "price_currency": null,
  "transmission": "manual",
  "updated_at": "2020-10-22T11:55:03.155530",
  "url": null,
  "vin": null
}

If I need to get items in vechile, I can use the following:

select v.* 
from json_data,
lateral flatten( input => j:"vehicle" ) v;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM