0

We are trying to extract certain information of multiple json objects written into a file. There is one per line, and need to get some strings from each of them and print specific information.

This JSON format is very strange, I would like to know if there is a code to achieve results we need

{'creationDate': None, 'endTime': 1563754500.0, 'expirationDate': 1563743700.0, 'isPriorityOffer': False, 'offerId': 'AAGutxx7466', 'offerType': 'NON_EXCLUSIVE', 'rateInfo': {'currency': 'USD', 'isSurge': False, 'priceAmount': 54.0, 'projectedTips': 0.0, 'surgeMultiplier': None}, 'schedulingType': 'BLOCK', 'serviceAreaId': 'dd00ctyy', 'serviceTypeId': 'PuyOplzlR1idvfPkv5138g', 'serviceTypeMetadata': {'nameClassification': 'STANDARD'}, 'startTime': 1563743700.0, 'startingLocation': {'address': {'address1': '', 'address2': None, 'address3': None, 'addressId': None, 'city': None, 'countryCode': None, 'name': None, 'phone': None, 'postalCode': None, 'state': None}, 'geocode': {'latitude': 0.0, 'longitude': 0.0}, 'locationType': None, 'startingLocationName': ''}, 'status': 'OFFERED', 'trIds': None}
{'creationDate': None, 'endTime': 1563754500.0, 'expirationDate': 1563741900.0, 'isPriorityOffer': False, 'offerId': 'AAGutxx8547', 'offerType': 'NON_EXCLUSIVE', 'rateInfo': {'currency': 'USD', 'isSurge': False, 'priceAmount': 63.0, 'projectedTips': 0.0, 'surgeMultiplier': None}, 'schedulingType': 'BLOCK', 'serviceAreaId': '50ade699', 'serviceTypeId': 'PuyOplzlR1idvfPkv5138g', 'serviceTypeMetadata': {'nameClassification': 'STANDARD'}, 'startTime': 1563741900.0, 'startingLocation': {'address': {'address1': '', 'address2': None, 'address3': None, 'addressId': None, 'city': None, 'countryCode': None, 'name': None, 'phone': None, 'postalCode': None, 'state': None}, 'geocode': {'latitude': 0.0, 'longitude': 0.0}, 'locationType': None, 'startingLocationName': ''}, 'status': 'OFFERED', 'trIds': None}

I tried this code, but is not working wih that json structure

<?

function readJson($File){

  // open the file to with the R flag,
    $Path = fopen($File,"r");

    // if file found,
    if ($Path) {
        $print = '';

            // for each line
            while (($line = fgets($Path)) !== false) {
                $Output = json_decode($line);
                $print .= "Service Area: ".$Output->serviceAreaId."<br/>";
                $print .= "Start time: ".$Output->startTime."<br/>";
                $print .= "Price: ".$Output->priceAmount."<hr>";
            }

        fclose($Path);
    } 

    return $print;
}

echo readJson("logs.txt");

?>

3 Answers3

1

As mentioned, that's not valid JSON. You must use double quotes for keys and string values. If you can't change the files then you will need to fix it up:

$line = str_replace(["'", "None", "False"], ['"', 0, 0], $line);
$Output = json_decode($line);

If you have strings other than None and False that aren't quoted then you will have to add those to the str_replace arrays. If there aren't set ones (could be anything) then you'll need a regex. It's easier to change them to an integer than it is to try wrapping them in quotes.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • I've tried this, but still not working.. I added that line with str_replace, but is giving blank results. You can see it running here instakarts.com/panel.php – Code Unknown Jul 19 '19 at 17:51
  • https://3v4l.org/5oBWl Also, `priceAmount` is a property under `rateInfo`. – AbraCadaver Jul 19 '19 at 17:55
0

Please change your JSON-Strings to a valid JSON:

{"creationDate": null, "endTime": 1563754500.0, "expirationDate": 1563743700.0, "isPriorityOffer": false, "offerId": "AAGutxx7466", "offerType": "NON_EXCLUSIVE", "rateInfo": {"currency": "USD", "isSurge": false, "priceAmount": 54.0, "projectedTips": 0.0, "surgeMultiplier": null}, "schedulingType": "BLOCK", "serviceAreaId": "dd00ctyy", "serviceTypeId": "PuyOplzlR1idvfPkv5138g", "serviceTypeMetadata": {"nameClassification": "STANDARD"}, "startTime": 1563743700.0, "startingLocation": {"address": {"address1": "", "address2": null, "address3": null, "addressId": null, "city": null, "countryCode": null, "name": null, "phone": null, "postalCode": null, "state": null}, "geocode": {"latitude": 0.0, "longitude": 0.0}, "locationType": null, "startingLocationName": ""}, "status": "OFFERED", "trIds": null}

Also you have to access the price via

$print .= "Price: ".$Output->rateInfo->priceAmount."<hr>";

On my machine the code is running after changing

0

Try this

I would grab the whole thing and then manipulate with the data. And when you pass it back to the file, do: json_encode($data, JSON_PRETTY_PRINT);, so you have a better overview of your json file.

grabbing data:

    <?php
        $f = fopen($file, "r");
        $fr = fread($f, filesize($file));
        $arr = json_decode($file, true);
        $fclose($f);
    
        // access example
        $arr["username"]["id"];

At the moment i am working on a similar project with json and it worked flawlessly for me.

Links

json_decode()
json_encode()

Community
  • 1
  • 1
era-net
  • 387
  • 1
  • 11