sqljsonvisual-studio-codesnowflake-cloud-data-platform

VS Code not recognizing string with brackets in SQL code


I am using SQL and trying to extract data from a JSON field that has a "key" name with brackets ([]) in it. The brackets do not represent an array, they are simply part of the string/name/identifier. Example contents of the JSON column:

{
  "status": 1,
  "otherfieldname[1]": 2.5
}

The following works effectively in Snowflake SQL to extract the values, but it appears to confuse VS Code:

SELECT
    message['status']::int as status 
    , message['otherfieldname[1]']::float as field_1 
FROM
    table_with_message_JSON

Here's how VS code looks: screenshot of VS Code UI not recognizing the string. It seems to think there is a missing single quote ', as it treats everything after the 1] as text. Is this perhaps a setting that I could change in VS Code? Or is there another way to write the SQL to extract the field value?

To attempt to resolve the issue, I have tried \ excluding the brackets, and several other methods of getting JSON data, to no avail.


Solution

  • Since you are writing Snowflake SQL, the regular "SQL" language Visual Studio Code supports doesn't interpret this very well because its not designed for Snowflake SQL (Version 1.93 actually updated the language name from "SQL" to "MS SQL" to better reflect this).

    Install the Snowflake extension. Your SQL will look much more appropriate afterwards when you select "Snowflake SQL" as your language server:

    snowflake SQL example

    You can change your language server by pressing Ctrl + K, M and select "Snowflake SQL".