I want to be able to extract all values from json in snowflake contains "_sum".
I have this format for example:
{"260196":7877,"260206":2642,"260216":7620,"260226":7560,"_sum":25699}
{"260196":9804,"260206":9804,"260216":9487,"260226":9411,"_sum":38506}
I want this results:
Total | Value |
---|---|
_sum | 25699 |
_sum | 38506 |
I try to used this:
, TO_VARCHAR(GET_PATH(PARSE_JSON(x), '_sum'))
but I received error
100069 (22P02): Error parsing JSON: unknown keyword "N", pos 2
2
Answers
What about something like this:
I see:
Then to get your desired output:
I get:
Much like’s Sergu’s answer:
using the VALUES to have two strings "that are JSON" we can PARSE_JSON like you have then, access it via the
:
accessor, or viaget_path
form. Your code works just fine on this example data, and then we can change ether access method (s1, s2) to TEXT (the same thing as VARCHAR) via inline case::
gives all working stuff as expected:
So the error message you post means, "the text you have is not JSON" in simplest form:
gives:
so your "JSON" is not valid JSON, if you are expect some bad input you can use the TRY_PARSE_JSON function, which returns NULL on failure to parse. But if you are not expected bad data, it sounds like you need to find what JSON strings you have the start with a
N
.It makes me think of JOSNL that has been split perhaps wrongly, or something like that? perhaps a newline in the JSON text, that then got stringfied, and thus became a
\N
type thing?