I have a problem parsing a certain nested JSON structure in Snowflake. The problem here is the usage of special characters like @ and # for example for some of the elements. Those characters prevents me from using simple dot notation when trying to access some of the elements without resourcing to a really complicated queries utilizing joins and where clauses on the flattened parts of the structure. Here’s an example of how the JSON file looks like:
{ "ContractTerm": [ { "@ID": 123123, "CodeTermTypeID": { "#text": "some_text 123123" }, "ContractID": { "#text": "other_text 123123", "@href": "/businessObject/123123", "@ID": 123123 }, "ContractTermID": 123123 }, { "@ID": 234234, "CodeTermStatusID": { "#text": "some_text_again 234234" }, "CodeTermTypeID": { "#text": "some_text 234234" }, "ContractID": { "#text": "some_other_text 234234", "@href": "/businessObject/234234", "@lxID": 234234 }, "ContractTermID": 234234 }, { "@lD": 345345, "CodeTermTypeID": { "#text": "another_text 345345" }, "ContractID": { "#text": "another_text 345345", "@href": "/businessObject/345345", "@lxID": 345345 }, "ContractTermID": 345345 } ] }
Is it possible to get those elements starting with @ and # like using some escape characters or something similar in SQL code?
Advertisement
Answer
Use quotes around the attributes with special characters. For example:
WITH x as ( SELECT parse_json('{ "ContractTerm": [ { "@ID": 123123, "CodeTermTypeID": { "#text": "some_text 123123" }, "ContractID": { "#text": "other_text 123123", "@href": "/businessObject/123123", "@ID": 123123 }, "ContractTermID": 123123 }, { "@ID": 234234, "CodeTermStatusID": { "#text": "some_text_again 234234" }, "CodeTermTypeID": { "#text": "some_text 234234" }, "ContractID": { "#text": "some_other_text 234234", "@href": "/businessObject/234234", "@lxID": 234234 }, "ContractTermID": 234234 }, { "@lD": 345345, "CodeTermTypeID": { "#text": "another_text 345345" }, "ContractID": { "#text": "another_text 345345", "@href": "/businessObject/345345", "@lxID": 345345 }, "ContractTermID": 345345 } ] }') as var) SELECT y.value:"@ID" FROM x, LATERAL FLATTEN(input=>x.var:ContractTerm) y