Ezh
Ezh

Reputation: 629

Correct encoding for JSON numbers in PHP

Let's say I have such JSON:

{
 id: 1,
 somevalue: "text"
}

and I want to create this JSON by PHP function json_encode. I can pretty easy get this JSON in form:

{
 "id": "1",
 "somevalue": "text"
}

or, using JSON_NUMERIC_CHECK, in form where "id" will be numeric, but "somevalue" can be either numeric or text, depend on its content.

How can I make JSON where "somevalue" always be in text format (with quotes). I'll parse it by other language, where it is important.

Upvotes: 0

Views: 208

Answers (3)

knagode
knagode

Reputation: 6125

On PHP> 5.3.3., you can use json_decode($array, JSON_NUMERIC_CHECK);

If you do not have PHP 5.3.3 or more I wrote this recursive function:

function json_encode_with_numbers($array) {
    if(is_array($array)) {
        if(count($array)>0 && array_keys($array) !== range(0, count($array) - 1)) {
            echo '{'; 

            $isFirst = true;
            foreach($array as $key=>$item) {
                if(!$isFirst) {
                    echo ",";
                }
                echo '"'.$key.'":';
                json_encode_with_numbers($item);
                $isFirst = false;
            }
            echo '}';
        } else {
            echo '['; 
            $isFirst = true;
            foreach($array as $item) {
                if(!$isFirst) {
                    echo ",";
                }
                json_encode_with_numbers($item);
                $isFirst = false;
            }
            echo ']';
        }
    } else {
        if(is_numeric($array)) {
            echo $array;
        } elseif ($array == null) {
            echo "null";
        } else {
            echo '"'.str_replace(array('"', '\\'), array('\"', '\\\\'), $array).'"'; // escape special chars
        }

    }

}

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

Make sure values you want to be non string (like int or boolean) are entered as such into your source array:

<?php
$a = array('id' => 1, 'really' => true, 'somevalue' => 'text');
var_dump( json_decode( json_encode( $a ) ) );

gives expected:

object(stdClass)#1 (2) {
  ["id"]=>
  int(1)
  ["really"]=>
  bool(true)
  ["somevalue"]=>
  string(4) "text"
}

EDIT

If you want it always to be strings, then put strings in your array in first place:

<?php
$a = array('id' => '1', 'really' => 'true', 'somevalue' => 'text');
var_dump( json_decode( json_encode( $a ) ) );

would give

object(stdClass)#1 (3) {
  ["id"]=>
  string(1) "1"
  ["really"]=>
  string(4) "true"
  ["somevalue"]=>
  string(4) "text"
}

but that kills the whole purpose of having different variable types.

Of you can convert your array prior json encoding:

<?php
$a = array('id' => 1, 'really' => true, 'somevalue' => 'text');

$tmp = array();
foreach( $a as $key=>$val ) {
   $tmp[$key] = (string)$val;
}

var_dump( json_decode( json_encode( $tmp ) ) );

would give in the end:

object(stdClass)#1 (3) {
  ["id"]=>
  string(1) "1"
  ["really"]=>
  string(1) "1"
  ["somevalue"]=>
  string(4) "text"
}

Upvotes: 4

lukedays
lukedays

Reputation: 323

To make somevalue always in "text" format:

$somevalue1 = 1;
$somevalue2 = "text";

$json1 = array("id" => 1, "somevalue" => (string) $somevalue1);
$json2 = array("id" => 1, "somevalue" => (string) $somevalue2);

echo json_encode($json1); // outputs {"id":1,"somevalue":"1"}
echo json_encode($json2); // outputs {"id":1,"somevalue":"text"}

Upvotes: 2

Related Questions