-7

I have been stuck in this issue and cant seem to fix it.. I have this JSON STRING

$unBillableArr = ["{'id' : '123','to' : '+923412268656','MsgReceivedFrom' : '03349433314', 'message':'Qwertyy ', 'recdate':'2017-11-20 19:01:49'}"];

I need to convert it into array of object or maybe just one json object.. I have tried doing

json_decode($unBilledArr , true);

It gives me null.

Already tried these solutions as well

Convert a string to JSON object php

https://jonsuh.com/blog/convert-loop-through-json-php-javascript-arrays-objects/

I must be doing something wrong.. cant seem to figure out what..

P.s : This is the response i am getting and i can not do anything about the response.

14
  • 7
    That's an array containing an json string - maybe you should remove the [] around the "real" json-string Commented Nov 20, 2017 at 15:58
  • 5
    @Philipp yes and he should use right variable $unBillableArr <> $unbilledArray Commented Nov 20, 2017 at 15:59
  • 2
    That's not an array of objects. That's just one json object you've put in a PHP array as a string. Commented Nov 20, 2017 at 15:59
  • 2
    Your JSON string also uses single quotes, which json_decode can't work with. Commented Nov 20, 2017 at 16:00
  • 2
    So what you get is not actual JSON (wrong kinds of quotes)? Then you should really ask the supplier of this data to send standardised valid JSON; or also supply a custom decoder for PHP. Commented Nov 20, 2017 at 16:03

1 Answer 1

1

You are trying to decode an array, either specify the specific item within the array, or make it a string.

For instance:

$unBillableArr = '{"id":"123", "to":"+923412268656", "MsgReceivedFrom":"03349433314", "message":"Qwertyy", "recdate":"2017-11-20 19:01:49"}';

$json = json_decode($unBillableArr, true);

// Or

$unBillableArr = ['{"id":"123", "to":"+923412268656", "MsgReceivedFrom":"03349433314", "message":"Qwertyy", "recdate":"2017-11-20 19:01:49"}'];

$json = json_decode($unBillableArr[0], true);

However, given the string you are receiving does not contain valid JSON and you are unable to control what you receive, I have prepared the following that will replace the single quotes in your JSON, and decode each item into an array:

$unBillableArr = ["{'id' : '123','to' : '+923412268656','MsgReceivedFrom' : '03349433314', 'message':'Qwertyy ', 'recdate':'2017-11-20 19:01:49'}"];

// Loop through each JSON string
$jsonObjects = []; // Change this name...
foreach ($unBillableArr as $jsonString) {
    $jsonObjects[] = json_decode(str_replace("'", '"', $jsonString), true);
}

print_r($jsonObjects); // Proof it works

Please bear in mind that I would consider this to be a dirty fix. To fix this properly, you should go back to the source and ensure that the JSON they are returning to you is valid.

Sign up to request clarification or add additional context in comments.

5 Comments

This answer does not work.. i have tried this in sandbox
@SyedAbdurRehmanKazmi it does work... Trust me! Make sure you are using the correct quotes, and make sure you are referencing the correct item in the $unBillableArr array
I cant change anything in this string this is the issue.. Otherwise i know it would work..
Thanks man.. @BenCarey i wanted a dirty fix. since i cant go back to main sources right now.. i told my supervisor that this is not a valid JSON and we need a proper JSON he told me to get a work around it until we are able to get in contact with them.
@SyedAbdurRehmanKazmi Find some better developers to work with :-P. Glad it sorted it, good luck!

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.