4

I have a JSON string that contains some key with the following value: 123.00. When I use json_decode function I get the decoded string where the previous key equal to 123, not to 123.00. Is there a way to correct decode such values without wrapping into quotes?

5 Answers 5

7

This is currently being brought up as a PHP bug:

Bug Report: https://bugs.php.net/bug.php?id=50224

In the future, there may be functionality to pass a flag through the options parameter for stricter typing. For now, however, wrapping it in quotes will have to suffice.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response and for the link!
Fixed as of PHP 7.0, new option available for json_decode, JSON_PRESERVE_FRACTIONAL_PART
I believe it should be JSON_PRESERVE_ZERO_FRACTION. See respective commit
1

I do not think it is possible!

Comments

1
//convert the json to a string before json_decode
$res = preg_replace( '/next_cursor":(\d+)/', 'next_cursor":"\1"', $json );

Comments

0
number_format($number, 2) 

output the number through that?

Comments

0

You can use the JSON_BIGINT_AS_STRING option, for example:

$json = json_decode($input, true, 512, JSON_BIGINT_AS_STRING);

Careful though, this only works with PHP 5.4+!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.