Skip to content
Advertisement

issues converting output from a view to json and array in snowflake using SQL

I am new to snowflake and have successfully created a view from a table using sql, but I am having issues creating a view that transforms the whole table into Json and array

my view

create or replace  view my_view as (
  select id, town, created_date, updated_at, array_construct( 
 object_construct('service','green','period', 
 object_construct('Type',type,'end_date', end_date)))Services
from demo
);

my_view output

id town created_date updated_at Services
123 modak 2024-03-29 2024-03-29 [{ “service”: “green”, “period”:{“Type”: “definite”, “end_date”: “2024-03-29 11:17:42.000”}}]

my objective is to create two views from my_view that will do the following:

  1. convert my_view to json
  2. Convert my_view to array

The below codes are able to convert my_view to json and array successfully

select array_agg(object_construct(*)) from my_view; 

select object_construct(*) from my_view; 

But when I try to create a view with it I get errors

create or replace  view my_json as (
  select object_construct(*) from my_view
);

error generated

SQL compilation error: Missing column specification

Advertisement

Answer

The expression has to be aliased:

create or replace  view my_json as (
  select object_construct(*) AS output from my_view
);

or:

create or replace view my_json(output) as (
  select object_construct(*) from my_view
);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement