Force PHP to passthrough numbers in original format

14 views Asked by At

Im using PHP import_json to open a JSON file, then export_json to output just the first item as another JSON file (essentially splitting one long JSON job list into individual jobs) but I'm having a problem where somewhere along the line numbers that are real or floating point numbers with trailing ".0" are being converted and output as integers.

Example:

"Quality": -3.0,
"Quality": 20.0,
"AudioTrackGainSlider": 0.0,
"AudioTrackDRCSlider": 0.90000000000000002,

Comes out as:

"Quality":-3,
"Quality":20,
"AudioTrackDRCSlider":0.9,
"AudioTrackGainSlider":0,

The entire PHP code I'm currently using:

#open JSON file

$inputjson = json_decode(file_get_contents("store/queues/queue.json"), true);

#if empty echo "null"

if (is_null($inputjson[0])) {
echo "NULL";
} else {

#output first array index and print

header('Content-type: application/json');
$encodejson = array(0 => $inputjson[0]);
printf(json_encode($encodejson));

#Modify job list to remove first object and update index numbers

$outputjson = array();
$itemcount = count($inputjson);
$iterator = 1;
while ($iterator < $itemcount) {
    $adjustment = $iterator - 1;
    $pusharray = $inputjson[$iterator];
    array_push($outputjson,$pusharray);
    $iterator++;
}

#remove and re-create queue file with new generated queue

shell_exec('rm store/queues/queue.json');
shell_exec('touch store/queues/queue.json');
$jsoncreate = fopen("store/queues/queue.json", "w") or die("Unable to Write");
fwrite($jsoncreate, json_encode($outputjson));
fclose($jsoncreate);
}

Example JSON at: https://pastebin.com/K2qnggjT

Unfortunately the program that I'm making the single job JSON for requires the numbers be formatted correctly, or I get a "expected real but got int" error. The JSON files contain strings and numerical values, and there are nested arrays in there as well.

Is there any way to have PHP just passthrough the numbers as if they were strings, but still show as numerical values in the result? In the source there are numbers that show as integers, so I don't think that just defining all numbers as floating point with trailing .0's will work. Is this possible with PHP at all?

0

There are 0 answers