Not able to create exact .json from a struct

5 views (last 30 days)
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
Mert Asil Türeli
Mert Asil Türeli on 23 Jun 2021
"features" also needed to be a table. Server returns now internal error instead of bad request. So I assume both .json's are now identical.

Sign in to comment.

Accepted Answer

Mert Asil Türeli
Mert Asil Türeli on 23 Jun 2021
Edited: Mert Asil Türeli on 23 Jun 2021
I finally figured it out. If anyone else faces a similar issue in future, here is the code snippet to generate the .json below.
% Area example
area_json_struct = struct();
area_json_struct.task_type = "area";
area_json_struct.task_name = "test";
%% 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 = "native";
% json_struct -> params -> Geo
area_json_struct.params.geo.type="FeatureCollection";
area_json_struct.params.geo.fileName="User-Drawn-Polygon";
%% features
type = "Feature";
properties = struct();
geometry.type = "Polygon";
geometry.coordinates = [...
[5,12];...
[5,13];...
[6,13];...
[6,12];...
[5,12]...
];
area_json_struct.params.geo.features = table(type, properties, geometry);
area_payload = jsonencode(area_json_struct, 'PrettyPrint', true);
which prints:
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": "native"
},
"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
]
]
}
}
]
}
}
}'

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!