Not able to create exact .json from a struct
5 views (last 30 days)
Show older comments
Mert Asil Türeli
on 23 Jun 2021
Commented: Mert Asil Türeli
on 23 Jun 2021
Hello, I need to create a .json to make a request using an API. Using Python, it worked succesfully but with MATLAB I constantly get status 400 with "Bad Request". Since creating a .json in MATLAB is not as easy as in Python , I may be making some mistake creating the .json. Also, I already made another kind of request via MATLAB using .json to the same server, which worked fine. Here is the .json, that I need to create:
new_task = {
"task_type": "area",
"task_name": "area_request1",
"params":
{
"dates": [
{
"startDate": "01-01-2018",
"endDate": "02-14-2018"
}],
"layers": [
{
"layer": "SRTMGL3_DEM",
"product": "SRTMGL3_NC.003"
}],
"output":
{
"format":
{
"type": "geotiff"
},
"projection": "geographic"
},
"geo":
{
"type": "FeatureCollection",
"fileName": "User-Drawn-Polygon",
"features": [
{
"type": "Feature",
"properties":
{},
"geometry":
{
"type": "Polygon",
"coordinates": [
[
[5, 12],
[5, 13],
[6, 13],
[6, 12],
[5, 12]
]
]
}
}]
}
}
}
And here is the code to construct it in MATLAB:
% Area example
area_json_struct = struct();
area_json_struct.task_type = "area";
area_json_struct.task_name = "area_request1";
%% json_struct -> params
% json_struct -> params -> dates
startDate = ["01-01-2018"];
endDate = ["01-02-2018"];
area_json_struct.params.dates = table(startDate, endDate);
% json_struct -> params -> layers
layer = ["SRTMGL3_DEM"];
product = ["SRTMGL3_NC.003"];
area_json_struct.params.layers = table(layer, product);
% json_struct -> params -> output
area_json_struct.params.output.format.type = "geotiff";
area_json_struct.params.output.projection = "geographic";
% json_struct -> params -> Geo
% Siehe https://de.wikipedia.org/wiki/GeoJSON
area_json_struct.params.geo.type="FeatureCollection";
area_json_struct.params.geo.fileName="User-Drawn-Polygon";
area_json_struct.params.geo.features.type = "Feature";
area_json_struct.params.geo.features.properties = table();
area_json_struct.params.geo.features.geometry.type = "Polygon";
area_json_struct.params.geo.features.geometry.coordinates = [...
[5,12];...
[5,13];...
[6,13];...
[6,12];...
[5,12]...
];
area_payload = jsonencode(area_json_struct, 'PrettyPrint', true);
which results in:
area_payload =
'{
"task_type": "area",
"task_name": "test",
"params": {
"dates": [
{
"startDate": "01-01-2018",
"endDate": "01-02-2018"
}
],
"layers": [
{
"layer": "SRTMGL3_DEM",
"product": "SRTMGL3_NC.003"
}
],
"output": {
"format": {
"type": "geotiff"
},
"projection": "geographic"
},
"geo": {
"type": "FeatureCollection",
"fileName": "User-Drawn-Polygon",
"features": {
"type": "Feature",
"properties": [],
"geometry": {
"type": "Polygon",
"coordinates": [
[
5,
12
],
[
5,
13
],
[
6,
13
],
[
6,
12
],
[
5,
12
]
]
}
}
}
}
}'
Thank you for any help!
6 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on JSON Format in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!