json char with too many decimals, need removal
Show older comments
I got some json str = '{"hund": 0.3253533250000000000000000000000000, "kat": "dfsdfs", "baenkebider": 0.002021203321320000000000000000000000}';
In reality its a longer string with even more figures. I need to remove all those long decimals. 7-8 decimals is enough, so I end up with something like:
str = '{"hund": 0.3253533, "kat": "dfsdfs", "baenkebider": 0.0020212}';
If someone can help with an elegant solution I will appreciate it a lot!
Thanks in advance,
-best
mergh
3 Comments
dpb
on 30 May 2021
>> split(extractBetween(str,'{','}'),',')
ans =
3×1 cell array
{'"hund": 0.3253533250000000000000000000000000' }
{' "kat": "dfsdfs"' }
{' "baenkebider": 0.002021203321320000000000000000000000'}
>> str2double(extractAfter(ans,':'))
ans =
0.3254
NaN
0.0020
>>
Use join() to put the isfinite pieces back together.
Alternatively, although it is crude, you could just do a string subsitution for some arbitrary number of consecutive zeros with empty or simply pick up the leftmost N characters of the numeric values.
A regular expressions maven could undoubtedly write a very clever pattern, but that isn't me... :)
Martin
on 30 May 2021
dpb
on 30 May 2021
I knew Stephen or similar would be along...
You'll note I specifically did NOT say it was elegant... :)
Accepted Answer
More Answers (0)
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!