Clear Filters
Clear Filters

jsondecode not suitable to parse uint64 array values

10 views (last 30 days)
Why jsondecode function is not able to correctly parse uint64 values? Is MathWorks going to solve this issue?
I was reading a json file with timestamps in nanoseconds:
json_data = fileread('my_json_file.json');
data = jsondecode(json_data);
When accessing to the value in matlab, it returned 1704972586391907328. My original value was 1704972586391907240.
So I asked myself, how is this possible. It turns out that what "jsondecode" function is doing is:
str2double(raw_values_str)
As Numerics of class double only have 15 decimal digits of precision (52 bits), it is not precisely representable using a double.
To be able to retrieve the uint64 value I did a manual parsing of json file:
function values = extract_uint64_array(json_data, field_name)
% Construct the field search string
field_search_str = sprintf('"%s": [', field_name);
% Extract the specified field manually
start_index = strfind(json_data, field_search_str) + length(field_search_str);
end_index = strfind(json_data(start_index:end), ']') - 2 + start_index;
raw_values_str = json_data(start_index:end_index);
% Convert the field values to uint64 array using sscanf and a loop
raw_values_cell = strsplit(raw_values_str, ',');
values = uint64(zeros(1, numel(raw_values_cell)));
for i = 1:numel(raw_values_cell)
values(i) = sscanf(raw_values_cell{i}, '%lu');
end
end
I hope it helps some of you. Thank you for reading!

Answers (1)

Sachin Lodhi
Sachin Lodhi on 12 Jun 2024
Hello GonzaloMadrono,
I understand that you are trying to parse uint64 values using "jsondecode" function. I too was facing this issue and with the following workaround I was able to accurately parse uint64 values from JSON data: 
function values = extract_uint64_array(json_data, field_name)
% Construct the field search string
field_search_str = sprintf('"%s": [', field_name);
% Extract the specified field manually
start_index = strfind(json_data, field_search_str) + length(field_search_str);
end_index = strfind(json_data(start_index:end), ']') - 2 + start_index;
raw_values_str = json_data(start_index:end_index);
% Convert the field values to uint64 array using sscanf and a loop
raw_values_cell = strsplit(raw_values_str, ',');
values = uint64(zeros(1, numel(raw_values_cell)));
for i = 1:numel(raw_values_cell)
values(i) = sscanf(raw_values_cell{i}, '%lu');
end
end
This function manually extracts the array of uint64 values from the raw JSON data, ensuring accurate representation of the large integers.
I hope this helps!

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!