Jump to content

Using float values in JSON


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
https://forums.phpfreaks.com/topic/302820-using-float-values-in-json/
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.

The receiving machine needs float values.

 

Again: Why?

 

As I already said, PHP and virtually all other mainstream languages can convert integer types on the fly:

var_dump(128 + 0.5);    // float(128.5)

So what kind of application “needs floats” and is unable to handle 128 as opposed to 128.0?

As said, PHP does not distinguish between 123 and 123.0. If you must have 123.0 then you need to do that manually. Which would suck.

 

Are you absolutely sure it has to be a float? Have you tried an integer and the service is returning an error?

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

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.

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...

  • Solution

The client essentially subverts the type flexibility of InfluxDB by appending an explicit “i” whenever it encounters a PHP integer.

 

If there's no better client, cast the arguments of the Point constructor:

$point = new Point($measurement, (float) $value);

Or change the source code.

  • Like 1
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.