1

There are similar questions I have not understood explanation (my question is simpler..) inside php :

echo json_encode(array("value" => $data));

returns me a JSON line like:

{"value":"100"} (assuming 100 is $data)

How can I get a json like :

{"value":100} ($data NOT doublequoted) ??

Thank you

CLAbeel
  • 1,078
  • 14
  • 20
Freddy
  • 11
  • 2
  • 1
    You're probably after the `JSON_NUMERIC_CHECK` encode option. – Jonnix Nov 16 '16 at 15:05
  • Technically, all JSON in PHP should be wrapped in "" (double quotes), even the PHP-variables. PHP doesn't differentiate between "100" and 100, so I don't really understand the need. – junkfoodjunkie Nov 16 '16 at 15:08
  • JSON_NUMERIC_CHECK works IF the element of the array is a number, if like my case it is a variable, array("value" => $data) it doesn't work , still return the numeric value of $data doublequoted, any workaround ? – Freddy Nov 17 '16 at 07:43
  • YOU ARE CORRECT ! JSON_NUMERIC_CHECK works also if the element is a variable !! My mistake the $data variable content was not pure numeric THANKS – Freddy Nov 17 '16 at 08:26

1 Answers1

4

You should probably use JSON_NUMERIC_CHECK as the 2nd argument to json_encode() method of PHP like this:

$arr = ['value' => '100'];
$json_encoded = json_encode($arr, JSON_NUMERIC_CHECK);
// echo $json_encoded --- "{"value":100}"

Hope this helps!

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45