guys,
I'm building a JSON object from an interpolated string, and not getting how escaping works. I have to use double quotes for the API.
This does not interpolate the expressions between the curly braces:
@"{{
""name"":""{taskName}"",
""products"": [
{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]},
{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
],
""recurring"":true,
""query"": {
""date_from"": ""{dateFromString}"",
""date_to"": ""{dateToString}"",
""aoi"": {polygon}
},
""aoi_coverage_percentage"":90
}}";
This throws a bunch of errors-apparently, the curly brackets are not being escaped properly:
$"{{
""name"":""{taskName}"",
""products"": [
{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]},
{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
],
""recurring"":true,
""query"": {
""date_from"": ""{dateFromString}"",
""date_to"": ""{dateToString}"",
""aoi"": {polygon}
},
""aoi_coverage_percentage"":90
}}";
How should I format it in order to preserve the internal double quotes and outer brackets while allowing for the values inside the single brackets to be interpolated?
It seems that you have missed escape for the products
and query
objects:
$@"{{
""name"":""{taskName}"",
""products"": [
{{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}},
{{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}}
],
""recurring"":true,
""query"": {{
""date_from"": ""{dateFromString}"",
""date_to"": ""{dateToString}"",
""aoi"": {polygon}
}},
""aoi_coverage_percentage"":90
}}";
Just in case someone else is considering doing the same, it would be better to create an anonymous type and serialize it to json for two reasons:
taskName
has a double quote?)Below uses json.net for serialization.
var jsonObj = new {
name = taskName,
products = new[] {
new { product = "ndvi_image", actions = new [] { new { mapbox = "processed" } },
new { product = "true_color", actions = new [] { new { mapbox = "processed" } }
},
recurring = true,
query = new {
date_from = dateFromString,
date_to = dateToString,
aoi = polygon
},
aoi_coverage_percentage = 90
};
var jsonString = JsonConvert.SerializeObject(jsonObj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With