Search code examples
phpjsonunicodedecodingtext-parsing

Parse json string which is broken due to unicode characters and slashes


I'm trying to received data from Android to Laravel using API. Below are the data that I received in format object array list.

$obj = '{ "registerDetails": "{"accountName":"hh h hc","accountNumber":"868686","addressUser":"cg h jc","bankId":1,"selectedCityId":1,"emergencyName":"g g h","emergencyNumber":"0686868","education":[{"certificatePicture":{"body":{},"headers":{"namesAndValues":["Content-Disposition","form-data; name\u003d\"certificateFile\"; filename\u003d\"2022-11-30-00-56-28-598.jpeg\""]}},"grade":"hxycucu","educationLvl":"STPM / A Level or Equivalent","endEducation":"Dec 2022","id":4,"institutionName":"yxyxyxy","isEducationExpandable":false,"startEducation":"Nov 2022"}],"skill":[{"id":8},{"id":10}],"work":[{"companyName":"cuvuv","jobEndDate":"Nov 2022","isCurrentlyWorkHere":true,"isJobExpand":true,"jobPosition":"g cuc","jobScope":"cuccu","jobTitle":"6ff7f","jobStartDate":"Oct 2022"}],"phoneUser":"906886","postCode":"058686","selectedStateId":1}", "myKadFile": {}, "selfieFile": {} }';

I already attempted to decode the object, but it returned NULL.

Here's what I tried:

var_dump(json_decode($obj, true));

Solution

  • Bruteforce trial and error json repair at its finest:

    The problem stems from those pesky unicode = (\u003d) characters.

    I needed to replace them and handle slashes.


    ...In case I need to clarify for anyone...

    THIS IS A HACK!!!!

    Code: (Demo)

    $fixed = preg_replace('/\\\\u003d\\\\\\\\(.*?)\\\\\\\\/', '=\\\\\\\\\\\\$1\\\\\\\\\\\\', $obj);
    
    var_export(
        json_decode(json_decode($fixed)->registerDetails)
    );
    

    Output:

    (object) array(
       'accountName' => 'gsyau',
       'accountNumber' => '168454',
       'addressUser' => 'test',
       'bankId' => 1,
       'selectedCityId' => 1,
       'emergencyName' => 'test',
       'emergencyNumber' => '0146542346',
       'education' => 
      array (
        0 => 
        (object) array(
           'certificatePicture' => 
          (object) array(
             'body' => 
            (object) array(
            ),
             'headers' => 
            (object) array(
               'namesAndValues' => 
              array (
                0 => 'Content-Disposition',
                1 => 'form-data; name="certificateFile"; filename="2022-11-29-21-18-35-294.jpg"',
              ),
            ),
          ),
           'grade' => 'test',
           'educationLvl' => 'Doctoral (PHD) or Equivalent',
           'endEducation' => 'Oct 2022',
           'id' => 8,
           'institutionName' => 'test',
           'isEducationExpandable' => false,
           'startEducation' => 'Aug 2022',
        ),
      ),
       'skill' => 
      array (
        0 => 
        (object) array(
           'id' => 7,
        ),
        1 => 
        (object) array(
           'id' => 9,
        ),
      ),
       'work' => 
      array (
        0 => 
        (object) array(
           'companyName' => 'test',
           'jobEndDate' => 'Oct 2022',
           'isCurrentlyWorkHere' => false,
           'isJobExpand' => false,
           'jobPosition' => 'testtest',
           'jobScope' => 'test',
           'jobTitle' => 'test',
           'jobStartDate' => 'Aug 2022',
        ),
      ),
       'phoneUser' => '014654264',
       'postCode' => '68100',
       'selectedStateId' => 1,
    )