Jump to content

Using float values in JSON


NotionCommotion
Go to solution Solved by Jacques1,

Recommended Posts

b[0]->c is 128.  How can I make json_decode return it as a float and not an integer?  The only way that I am able to do so is hardcode 128 as either 128. or 128.0, but I don't wish to hard code it.  Thanks

<?php

$json='{"a": 123, "b": [{"c":127.2},{"c":128},{"c":129.2}], "d":123.321}';
echo '<pre>';var_dump(json_decode($json));echo '</pre>';
$json='{"a": 123, "b": [{"c":127.2},{"c":128.0},{"c":129.2}], "d":123.321}';
echo '<pre>';var_dump(json_decode($json));echo '</pre>';
$json='{"a": 123, "b": [{"c":127.2},{"c":128.},{"c":129.2}], "d":123.321}';
echo '<pre>';var_dump(json_decode($json));echo '</pre>';

function display($c1) {
    $arr=["a"=>123,"b"=>[["c"=>127.2],["c"=>$c1],["c"=>129.2]],"d"=>123.321];
    $json=json_encode($arr);
    echo '<pre>';var_dump(json_decode($json));echo '</pre>';
}
$c1=128;
display($c1);
$c1=(float) $c1;
display($c1);
$c1=128.0;
display($c1);

object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  array(3) {
    [0]=>
    object(stdClass)#2 (1) {
      ["c"]=>
      float(127.2)
    }
    [1]=>
    object(stdClass)#3 (1) {
      ["c"]=>
      int(128)
    }
    [2]=>
    object(stdClass)#4 (1) {
      ["c"]=>
      float(129.2)
    }
  }
  ["d"]=>
  float(123.321)
}
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  array(3) {
    [0]=>
    object(stdClass)#4 (1) {
      ["c"]=>
      float(127.2)
    }
    [1]=>
    object(stdClass)#3 (1) {
      ["c"]=>
      float(128)
    }
    [2]=>
    object(stdClass)#2 (1) {
      ["c"]=>
      float(129.2)
    }
  }
  ["d"]=>
  float(123.321)
}
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  array(3) {
    [0]=>
    object(stdClass)#2 (1) {
      ["c"]=>
      float(127.2)
    }
    [1]=>
    object(stdClass)#3 (1) {
      ["c"]=>
      float(128)
    }
    [2]=>
    object(stdClass)#4 (1) {
      ["c"]=>
      float(129.2)
    }
  }
  ["d"]=>
  float(123.321)
}
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  array(3) {
    [0]=>
    object(stdClass)#4 (1) {
      ["c"]=>
      float(127.2)
    }
    [1]=>
    object(stdClass)#3 (1) {
      ["c"]=>
      int(128)
    }
    [2]=>
    object(stdClass)#2 (1) {
      ["c"]=>
      float(129.2)
    }
  }
  ["d"]=>
  float(123.321)
}
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  array(3) {
    [0]=>
    object(stdClass)#2 (1) {
      ["c"]=>
      float(127.2)
    }
    [1]=>
    object(stdClass)#3 (1) {
      ["c"]=>
      int(128)
    }
    [2]=>
    object(stdClass)#4 (1) {
      ["c"]=>
      float(129.2)
    }
  }
  ["d"]=>
  float(123.321)
}
object(stdClass)#1 (3) {
  ["a"]=>
  int(123)
  ["b"]=>
  array(3) {
    [0]=>
    object(stdClass)#4 (1) {
      ["c"]=>
      float(127.2)
    }
    [1]=>
    object(stdClass)#3 (1) {
      ["c"]=>
      int(128)
    }
    [2]=>
    object(stdClass)#2 (1) {
      ["c"]=>
      float(129.2)
    }
  }
  ["d"]=>
  float(123.321)
}
Edited by NotionCommotion
Link to comment
Share on other sites

And that is important for what reason?

 

PHP converts numeric types on the fly whenever necessary, so insisting on a float 128.0 as opposed to an integer 128 makes no sense.

 

The receiving machine needs float values. I am in control of both the sending machine and receiving machine, and I wish to put as much of the processing on the sending machine.

 

Since I am in control of both machines, I didn't plan on validating on the receiving machine, but in hindsight, not doing so is likely never a good idea.

 

Please ignore.  I will send JSON in whatever format PHP wishes to do, and still validate/typecast on the receiving machine.

Link to comment
Share on other sites

One reason why you question makes no sense is that "128" can be a valid float value.

var_dump(floatval(128));
//Output: float(128)

If you must have the value "formatted" in a way that "looks" like a float (which seems doubtful) you can force the format using something like number_format();

$number = intval(128);
echo number_format ($number, 1);
//Output: 128.0

EDIT: Since you need to do that with array values you could apply that using array_walk() to all the values as needed.

Edited by Psycho
Link to comment
Share on other sites

Thanks Jacques1, requinix, and Psycho,

 

Yes, the version I am using will throw an error stating something like previous values were floats and the new value attempted to be inputted is an integer.  The problem manifests when a value just happens to be 128.0 which PHP replaces with 128.  I don't know whether influx just wants it to "look" like a float or really be one (and I am not sure this even makes any sense).

 

I haven't been able to check the influx version I am using, but expect it is old.  If it is, I will update it and hopefully this will be a mute issue.  If it still is an issue (which I expect will not be), I will just iterate over the values on the receiving side and typecast as necessary.

Link to comment
Share on other sites

I had been using influxdb 0.9.4.2.  I am also using https://github.com/influxdata/influxdb-php as a PHP influxdb client.  I never tried making an insert directly into the influx shell, so I don't know whether I would get the float/integer error.

 

I've since upgraded to influxdb 1.1.1.  I still get the float/integer error when using the influxdb-php client.  I do not get the error when making an insert directly into the influx shell.

 

Looks like I need a new influxdb client...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.