Preg_match_all question

Here is a snippet of a cURL result:

<?php
$json_result_from_curl = <<<EOD
{"status":"success","sku":{"items":"hammer","color":"green"},"refreshReady":false,"listing":[{"VendorId":"129","VendorName":"Allen","Price":78.75,"UpdatedOn":"2020-11-05 00:29:13"},{"VendorId":"16","VendorName":"Greg","Price":4.39,"UpdatedOn":"2020-11-05 02:15:06"},{"VendorId":"282","VendorName":"Carol","Price":0,"UpdatedOn":"2020-11-05 02:15:06"},{"VendorId":"5dd0","VendorName":"Susan","Price":.50,"UpdatedOn":"2020-11-05 02:15:06"},{"VendorId":"6","VendorName":"Thomas","Price":100.00,"UpdatedOn":"2020-11-05 02:15:06"}]}
EOD;

I figured out how to use preg_match_all to match what I want with this line of code:

preg_match_all('/VendorName":"(.*?)\",.+?([0-9\.]+)/',$data,$matches);

But how do I loop through to get the following result?

Allen 78.75
Greg 4.39
Carol 0
Susan .50
Thomas 100.00

It looks like you’re trying to parse some poorly encoded JSON. It’s broken because any number that is lower than one is being given as .56, rather than 0.56. It’s difficult to reliably parse JSON using regular expressions due to nesting, and the fact that the fields may not always come back in this order. I’d try to fix the response so it’s valid JSON, then work on that.

My preferred option would be to contact the provider of the API and tell them that their API is not valid JSON. Any number in JSON must start with the whole number portion, even if it’s zero. You’re unlikely to be the only person having this problem.

If the provider won’t help you or you’re in a hurry, you can find all broken prices in the JSON and fix them. For this example, you can do that like this:

$fixed_response = preg_replace('/"Price":(\.[\d.]+),/', '"Price":0$1,', $json_result_from_curl);

The pattern '/"Price":(\.[\d.]+),/' finds all the invalid Price declarations in the response, for example: "Price":.50,. The pattern '"Price":0$1,' fixes them by adding the 0, for example "Price":0.50,.

Once you have valid JSON, you can get the information you need by decoding the JSON and looping over the result:

foreach (json_decode($fixed_response)->listing as $listing) {
    echo $listing->VendorName . ' ' . $listing->Price;
}

Doing it this way means you can also check the status value in the response, to make sure you got what you expected before continuing.

This is your problem. No point doing anything until this is fixed.

It looks like they got the corrupted json code fixed (they fixed it rather quickly). However on my end, I now get a json “improperly formed” error because the cURL result always begins with the these characters:
{"status
So does this happen to y’all?
I guess the solution to this one is to do something like:
$data = curl_exec($ch);
$begin = strpos($data,’{“status’);
$data = substr($data,$begin);
$data = rtrim($data,’”’);
$proper_formatted_jason = json_decode($data, true);

Right?

So it’s still broken, just differently now. I’d get back on their case; I assume they’re trying to output JSON, so they’ll want to know. It also looks like they’re trying to create it manually, which is nuts. I don’t know how much sway you have with your provider, but maybe mention that every language has a JSON library available to use…

Sponsor our Newsletter | Privacy Policy | Terms of Service