how to read this type of JSON file from PHP [Json results from open alpr ]?

229 views Asked by At

I need to know how to extract JSON data like the JSON format posted below to php, Im trying to extract following Json file key value data to a mysql using a php script. Following JSON is from openalpr using alpr on command line exported as json

  "version": 2,
  "data_type": "alpr_results",
  "epoch_time": 1594858871000,
  "img_width": 3232,
  "img_height": 3232,
  "processing_time_ms": 522.5,
  "regions_of_interest": [],
  "results": [
    {
      "plate": "QAA1989C",
      "confidence": 90.09095,
      "matches_template": 0,
      "plate_index": 0,
      "region": "my",
      "region_confidence": 0,
      "processing_time_ms": 42.125999,
      "requested_topn": 10,
      "coordinates": [
        {
          "x": 1149,
          "y": 2071
        },
        {
          "x": 1836,
          "y": 2071
        },
        {
          "x": 1836,
          "y": 2268
        },
        {
          "x": 1149,
          "y": 2268
        }
      ]
    }
  ]
}

All I keep getting is errors like this Illegal string offset 'result The php code I tested to display the plate number: Can someone help guide me on how to get the values of a child array in json array using a PHP script? Thank you**

I used the following code not sure what the error is regarding the key value of child elements

$filename = "results.json";
          $data = file_get_contents($filename); //Read the JSON file in PHP
          $array = json_decode($data, true); //Convert JSON String into PHP Array
          
          foreach ($data['results']['results'] AS $d)
          {
            echo $d['plate'];
            }
1

There are 1 answers

2
Grocker On

your json string is wrong, The first line is missing the {

$json = '
{
    "version": 2,
    "data_type": "alpr_results",
    "epoch_time": 1594858871000,
    "img_width": 3232,
    "img_height": 3232,
    "processing_time_ms": 522.5,
    "regions_of_interest": [],
    "results": [
        {
            "plate": "QAA1989C",
            "confidence": 90.09095,
            "matches_template": 0,
            "plate_index": 0,
            "region": "my",
            "region_confidence": 0,
            "processing_time_ms": 42.125999,
            "requested_topn": 10,
            "coordinates": [
                {
                    "x": 1149,
                    "y": 2071
                },
                {
                    "x": 1836,
                    "y": 2071
                },
                {
                    "x": 1836,
                    "y": 2268
                },
                {
                    "x": 1149,
                    "y": 2268
                }
            ]
        }
    ]
}
';

$data = json_decode($json,true);

foreach ($data['results'] AS $d) {
    echo $d['plate'];
}