1

I'm trying to convert a Json string in php pulled from my mySQL database. I've been successful with other strings like this using json_decode, but for some reason when I try decoding this string:

$Json ='{"S1": "15,2,0,4,0,0,1","S2": "50,0,99,1,5,1,1,0"}';

json_decode($Json, true);

I get the error 'Array to string conversion'.

I've been doing the same thing successfully with this string,

[{"group":"new","users":",12345678","S1":"56,3,0,0,0,6,0","S2":"0,0,49,0,4,0,0,0","enabled":"1","admin":"1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0"}]

I don't know what the differences between the two are, as they both are pulled from the same database, and both validate as Json successfully. Can someone tell me how I can successfully convert this so that I can get json_decode to work on it successfully? Thanks.

**update: Here's the actual code I'm using

$tmpUserJson = $row['JSONSettings’];    

$tmpUserJson='['.$tmpUserJson.']'; ///I’ve tried with and without brackets    

echo $tmpUserJson; //returns {"S1":"15,2,0,4,0,0,1","S2":"50,0,99,1,5,1,1,0”}    

$userJson = json_decode( $tmpUserJson , true);    

echo $userJson['S1']; // returns Notice: Array to string conversion    
5
  • [] makes an array. I've never seen the {} used for shorthand array construction but looks like it does the same thing. Commented Mar 23, 2017 at 23:47
  • I don't get this error, if I execute your code - are you sure, you post the code, which throws the error? Commented Mar 23, 2017 at 23:49
  • i tried the exact string but getting result without error... all i can think is mysql_escape_string just check if the string posted is exact what the output is from mysql... or the double quotes are escaped... Commented Mar 23, 2017 at 23:53
  • Couldn't put code in the comments, so I added to the question. Commented Mar 24, 2017 at 0:35
  • It was happening because I had originally been trying to echo the array. Commented Mar 24, 2017 at 0:43

2 Answers 2

2

I think you get it because you are trying to echo out an array. Try:

$Json ='{"S1": "15,2,0,4,0,0,1","S2": "50,0,99,1,5,1,1,0"}';

$Json = json_decode($Json);

foreach( $Json as $item ){
    echo $item. "<br />";
}

Or you can var_dump your results: $Json ='{"S1": "15,2,0,4,0,0,1","S2": "50,0,99,1,5,1,1,0"}';

$Json = json_decode($Json);

var_dump($Json);
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, glad I could help.
2
$userJson = json_decode( $tmpUserJson , true);// this line return an associative array    

echo $userJson; // echo can't echo out an array that's why you got that error

As @David mentioned, if you want to echo out $userJson then you have couple of options:
1: loop through the array elements e.g : foreach($userJson as $value){ echo $value;}
2 for testing purpose : you can view $userJson data by using var_dump($userJson) or print_r($userJson); Good luck

Comments

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.