Home > Software engineering >  Storing generic JSON in Cosmos DB
Storing generic JSON in Cosmos DB

Time:11-09

I have number of JSON objects that I want to store in Cosmos DB, and they all share the same header, fields, but the remaining of the JSON are not the same, and at runtime I have no way of knowing the format. The JSON header fields contains fields such as Id, partition key and collection name, so based on that it should be enough to store it in the correct place.

From what I have tried in the dot.net client, the JSON needs to be serialized to an Object model to call the CreateItemAsync function, but I want to store the generic JSON. I have done something similar in Elastic Search - so is there a way to do this with Cosmos DB client also?

CodePudding user response:

You can deserialize the JSON to a Dictionary<string, object>. Using that no information should be lost. Be aware to use the same serializer as the Cosmos SDK does as it uses it to serialize the object back to a JSON before sending it as request. By default the current SDK uses Newtonsoft unless you specified a custom serializer.

  • Related