Clear Filters
Clear Filters

jsonencode not right format

3 views (last 30 days)
Pete sherer
Pete sherer on 13 Apr 2023
Answered: the cyclist on 13 Apr 2023
Hi
Is there a way to control behavior of jsonencode. Example below, I want the run_id to be full integer. However jsone encode writes it in floating format.
apiData = struct;
apiData.run_id = 1720787;
apiData.run = "BSCR_sbt rerun: PT Jan01_2023";
apiData.run_node_ids = [20970];
apiData.scale_load_factor = 1000.1;
if length( apiData.run_node_ids)== 1
apiData.run_node_ids = { apiData.run_node_ids};
end
jsonInp = string( jsonencode( apiData, 'PrettyPrint', true))
I got below, which the api doesn't accept float for run_id
jsonInp =
"{
"run_id": 1.720787E+6,
"run": "BSCR_sbt rerun: PT Jan01_2023",
"run_node_ids": [
20970
],
"scale_load_factor": 1000.001
}"

Accepted Answer

the cyclist
the cyclist on 13 Apr 2023
Is it possible for you to store that number as an integer instead, for example
apiData.run_id = int64(1720787);
?
Because then
apiData = struct;
apiData.run_id = int64(1720787);
apiData.run = "BSCR_sbt rerun: PT Jan01_2023";
apiData.run_node_ids = [20970];
apiData.scale_load_factor = 1000.1;
if length( apiData.run_node_ids)== 1
apiData.run_node_ids = { apiData.run_node_ids};
end
jsonInp = string( jsonencode( apiData, 'PrettyPrint', true))
jsonInp =
"{ "run_id": 1720787, "run": "BSCR_sbt rerun: PT Jan01_2023", "run_node_ids": [ 20970 ], "scale_load_factor": 1000.1 }"

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!