Makes JSON strings (relatively) pretty
Mostly meant for structures with simple strings and arrays; gets confused and !!mangles!! JSON when strings contain [ ] { or }.
Yury Bondarenko (2021). prettyjson.m (https://github.com/ybnd/prettyjson.m), GitHub. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Thank Yury Bondarenko for sharing! Also Thank Martin Hultman for the useful modification.
Thank you, this worked really well for me. I also added the following code right before the final strjoin to make it even prettier by adding some spacing after colons and commas (except inside strings).
for i = 1:length(lines)
sublines = split(lines{i}, '"');
for j = 1:2:length(sublines)
sublines{j} = strrep(sublines{j}, ':', ': ');
sublines{j} = strrep(sublines{j}, ',', ', ');
end
lines{i} = strjoin(sublines, '"');
end
lines = deblank(lines);
This worked really well fro me. I see that it doesn't handle certain cases, but it worked 100% for me and will work for almost everyone. Thanks for sharing! Sanity restored.