0

I have an array of JSON data that i'd like to import. An example of what i'd call non-problematic JSON data would be:

[{
  "records": [{
    "timestamp": 1437805800,
    "Import": 1011546
  },{
    "timestamp": 1437805800,
    "Import": 1075864
  },{
    "timestamp": 1437805800,
    "Import": 1132356
  }]
}]

The problem that I am having though is that sometimes the data might be like this:

[{
  "records": [{
    "timestamp": 1437805800,
    "Import": 1011546e3
  },{
    "timestamp": 1437805800,
    "Import": 1075864e3
  },{
    "timestamp": 1437805800,
    "Import": 1132356e3
  }]
}]

Where 101546e3 = 101546x10^3 and this is where I am having issues as the default behavior of json_decode because it will cast these values to float and within that, it converts e3 as 000, or e5 as 00000 so for the first values above I would get back 1011546000, 1075864000, 1132356000. I can't tell that this value had been modified as it may be a valid value.

How am I able to retrieve the correct value (present within the JSON string before running it through json_decode) from this JSON data given that it may contain the string e within what should be an integer value?

7
  • 1
    So let me get this straight... you're getting 101546e3 as a numeric field in your JSON and you want to import it as a string. So what you want is a PHP string value that looks like "101546e3". Is that correct? Commented Sep 19, 2016 at 13:25
  • @Simba exactly correct. It comes in the JSON without quotes and in doing so json_decode parses it as a float which causes some unwanted behavior for me. Commented Sep 19, 2016 at 14:15
  • @RyanVincent Trying to put this simply, I can have 1011546, and 1055632e3 as example numbers. json_decode, somewhere along the line renders "e3" in an encapsulated value as 3 zeros and this is what I mean by being modified. It's not me making the zero's it is json_decode doing it so I can't tell if it's a valid value or not if I run the string through json_decode, only if I inspect the raw string. Commented Sep 19, 2016 at 14:20
  • @Simba So essentially, json_decode has the optional parameter JSON_NUMERIC_CHECK which forces float values? I am looking to force string values here. Commented Sep 19, 2016 at 14:24
  • Okay, then there's an easy short answer: No you can't. Just to be clear, the JSON string you've quoted contains a numeric value. It is a number. It is not a string. It can only seen as a string if it is enclosed with quote marks in the JSON string. You cannot force this value to be a string unless you modify the incoming JSON itself. Commented Sep 19, 2016 at 14:48

1 Answer 1

2

You have to pass JSON_NUMERIC_CHECK as second parameter in json_encode() function

For example

$numbers = array('+123123', '-123123', '1.2e3', '0.00001');
var_dump(
 $numbers,
 json_encode($numbers, JSON_NUMERIC_CHECK)
);
echo "Strings containing improperly formatted numbers".PHP_EOL;
$strings = array('+a33123456789', 'a123');

Refer PHP Doc for second parameter various option

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

2 Comments

It returns 1.0e-5 for last parameter.
Thanks for the input on this as I did not know about this parameter. That being said, you have tested this by explicitly stating that all values in the numbers array are strings however in my example the values are not string values but integers so are not encapsulated by quotes. I have no control over the source so am battling to figure out how to deal with the data unless it's manually parsing the string and thinking up a way to encapsulate these values.

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.