-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.

Syed Abdur Rehman Kazmi
  • 1,640
  • 3
  • 13
  • 30
  • 7
    That's an array containing an json string - maybe you should remove the `[]` around the "real" json-string – Philipp Nov 20 '17 at 15:58
  • 5
    @Philipp yes and he should use right variable $unBillableArr <> $unbilledArray – Fky Nov 20 '17 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. – M. Eriksson Nov 20 '17 at 15:59
  • I am using the right variable. that must be a typo.. @Fky – Syed Abdur Rehman Kazmi Nov 20 '17 at 16:00
  • 2
    Your JSON string also uses single quotes, which `json_decode` can't work with. – iainn Nov 20 '17 at 16:00
  • i said i need to CONVERT IT INTO ARRAY OF object.. i know its a string inside an array.. This is the response i am getting and i can not do anything about the response. – Syed Abdur Rehman Kazmi Nov 20 '17 at 16:01
  • 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. – deceze Nov 20 '17 at 16:03
  • 1
    @SyedAbdurRehmanKazmi If you've got a typo in your original question, then can you please edit it? Otherwise people are just going to find issues that aren't relevant. The variable names still don't match since the last change. – iainn Nov 20 '17 at 16:04
  • Thanks for mentioning.. i have fixed that.. @iainn – Syed Abdur Rehman Kazmi Nov 20 '17 at 16:08
  • @deceze this is the issue.. this is what i am getting and i have to live with this.. is there any way to get a solution out of this. – Syed Abdur Rehman Kazmi Nov 20 '17 at 16:09
  • @SyedAbdurRehmanKazmi, I will amend my answer to include a solution to correct your invalid json – Ben Carey Nov 20 '17 at 16:10
  • 1
    Since this is *not JSON*, we'd need to know what exactly it is. What is the specification for this "language"? Is the only difference to JSON that double and single quotes are… reversed? Will there ever be any sort of quotes inside those values? How will they be escaped, if at all? All these questions are already answered in JSON, which is why it's easy to work with. For this stuff here you'll have to invent your own parser, and/or haxx something together which may or may not break someday in the future when some unexpected unescaped character is being sent. – deceze Nov 20 '17 at 16:11
  • This still doesn't make sense. You said that you know it's a string inside an array - why are you doing that? A JSON string inside a PHP array isn't a useful data structure, and your code still says you're trying to decode a variable that doesn't exist. – iainn Nov 20 '17 at 16:11
  • @iainn If it was in my hand i would have def avoided it.. I am getting this response from a third party with whom i can not communicate.. This is the issue. i need a work around for it.. – Syed Abdur Rehman Kazmi Nov 20 '17 at 16:17

1 Answers1

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.

Ben Carey
  • 16,540
  • 19
  • 87
  • 169