I have a Cosmos database in Well-Defined schema mode. I have a Synapse Serverless SQL instance. I have documents in Cosmos with objects that contain fields with null values. When I create a View for the Container in SQL the null object values are appearing as 0, why would it do this?
Example:
Here's a doc in Cosmos (object highlighted in yellow with null fields):
When I read the document from cosmos db in serverless SQL pool I faced the same issue as shown below:
The null values in AddressDetails
are being replaced with 0. This suggests that the SQL Server engine (or the underlying JSON parser used by OPENROWSET) is interpreting line1, line2, and postcode as numeric fields (probably integers), as shown below:
And 0 is their default value. That may be the reason to get AddressDetails
in above format. You can use below query to transform the AddressDetails
field as required:
SELECT
JSON_QUERY(
CONCAT(
'{"line1":',
CASE WHEN JSON_VALUE(AddressDetails, '$.line1') <> '0' THEN JSON_VALUE(AddressDetails, '$.line1') ELSE 'null' END,
',"line2":',
CASE WHEN JSON_VALUE(AddressDetails, '$.line2') <> '0' THEN JSON_VALUE(AddressDetails, '$.line2') ELSE 'null' END,
',"postcode":',
CASE WHEN JSON_VALUE(AddressDetails, '$.postcode') <> '0' THEN JSON_VALUE(AddressDetails, '$.postcode') ELSE 'null' END,
'}'
)
) AS AddressDetails_Transformed
FROM OPENROWSET(
PROVIDER = 'CosmosDB',
CONNECTION = 'Account=csmsdbs;Database=db',
OBJECT = 'c1',
SERVER_CREDENTIAL = 'MyCosmosDbAccountCredential'
) AS [c1];
JSON_VALUE
: Extracts individual JSON fields from the AddressDetails
column using the JSON path ($.fieldName
).CASE
Logic: Replaces 0
with null
.CONCAT
: Constructs the JSON object as a string.JSON_QUERY
: Ensures the result is treated as valid JSON.You can get the output as shown below: