2

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));
3
  • I also try this.. json_decode(str_replace ('\"','"', $obj), true); still return NULL Commented Nov 29, 2022 at 15:45
  • also this $foo = utf8_encode($obj); $array = json_decode($foo, true); Commented Nov 29, 2022 at 15:47
  • Even this answer doesn't make the json string containing unicode valid. Commented Nov 30, 2022 at 4:31

1 Answer 1

2

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,
)
Sign up to request clarification or add additional context in comments.

2 Comments

great, thanks a lot! btw how to access "certificateFile" value in education->certificatePicture? I set true for json_decode(json_decode($fixed)->registerDetails, true); then loops the education to get the value of "certificateFile", but I have no idea
I'd parse that data with a regex or formatted scan. 3v4l.org/NGRMb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.