Query Array in JSON with Azure Synapse Serverless

Christopher Mühl 111 Reputation points
2023-02-08T18:33:06.2233333+00:00

Hello community,

I am currently trying to query arrays via Synapse Serverless. (Synapase Link to CosmosDB for MongoDB)

There is a lot of documentation on how to query objects inside arrays, but I am failing on the supposedly easier query without object.

My object looks like this:

{
	"_id" : ObjectId("abc123"),
	"Location" : {
		"type" : "Point",
		"coordinates" : [
			6.840024763111111,
			51.485867243777776
		]
	},
	"Images" : [
		"ImageFileName1",
		"ImageFileName2",
		"ImageFileName3",
		"ImageFileName4"
	]
}

In the first query I want to query the coordinates as single columns for longitude and latitude. There are always exactly 2 array elements (0 and 1).

As you can see in the screenshot (Synapse-1.png), I can query the array, but when I try to access the first element, I get zero back.

What am I doing wrong here?

Synapse-1

In the second query I want to query the array "Images" on single rows.

Like this:

Id | Image

abc123 | ImageFileName1

abc123 | ImageFileName2

abc123 | ImageFileName3

abc123 | ImageFileName4

Of course I found the documentation (https://learn.microsoft.com/en-us/azure/synapse-analytics/sql/query-cosmos-db-analytical-store?tabs=openrowset-credential#flatten-nested-arrays) where objects inside an array are queried in the desired way, but without an object inside, how can I do this query?

Thank you in advance and best regards

Christopher

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
{count} votes

Answer accepted by question author
  1. MartinJaffer-MSFT 26,161 Reputation points
    2023-02-09T20:35:46.8966667+00:00

    @Christopher Mühl Hello and welcome to Microsoft Q&A.

    I understand you are having trouble extracting simple values from an array vis Synapse Link for Cosmos DB for MOngoDB.

    At first, looking at your samples, everything seemed find and promised to be a real headscratcher. You are accessing things correctly. So then I figured I should question my assumptions. Is it 0-based index or 1-based index? No I figured you would have caught that. So I look closer.

    Coordinates is an array of floats/double/decimal. Not an array of strings.

    Your query expects Varchar (string) types.

    I'm thinking implicit conversion/typecasting is not happening. However, If that is the case, why didn't you have to convert Coordinates?

    I checked in the link you provided, underneath "CosmosDB to SQL type mappings". I found

    Nested object or array varchar(max) (UTF-8 database collation), serialized as JSON text

    This means it implicitly converted Coordinates for you, but does not implicitly convert referenced elements.

    So I believe you need to explicitly convert like you did for id .


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.