2

I have an array that contains a JSON string.

Array
(
    [0] => Array
        (
            [name] => Original
            [nutrients] => {"calories":{"value":2500,"operator":2},"protein":{"value":500,"operator":1},"carbs":{"value":200,"operator":0},"fat":{"value":50,"operator":0},"sugar":{"value":1,"operator":2}}
        )

    [1] => Array
        (
            [name] => Rest
            [nutrients] => {"calories":{"value":5000,"operator":2},"sugar":{"value":10,"operator":2}}
        )

)

I want to turn the whole array into a JSON string

echo json_encode($array);

But this throws a \ in front of all quotes

[{"name":"Original","nutrients":"{\"calories\":{\"value\":2500,\"operator\":2},\"protein\":{\"value\":500,\"operator\":1},\"carbs\":{\"value\":200,\"operator\":0},\"fat\":{\"value\":50,\"operator\":0},\"sugar\":{\"value\":1,\"operator\":2}}"},{"name":"Rest","nutrients":"{\"calories\":{\"value\":5000,\"operator\":2},\"sugar\":{\"value\":10,\"operator\":2}}"}]

This problem comes about because the nutrients value is already a JSON string.

How can I convert an array into a JSON string when it already contains JSON strings, while having no slashes in front of the quotes?

4
  • (1) Either write a custom JSON generator. (2) Or decode the jsonish strings in that array structure, then reencode everything. Commented Aug 31, 2015 at 2:13
  • Loop through array to assign each json string to a new array, replace with placeholder, json encode array, replace original json strings? Commented Aug 31, 2015 at 2:14
  • That's fine that it throws a `\` in front of the quotes. JSON properties can be quoted or not. You don't have to use the quotes if there isn't a space or character situation that does not conform to a normal property naming standards. Your problem is that you have entire Strings with JSON inside of them Commented Aug 31, 2015 at 2:15
  • json_encode() is usually used to encode PHP associative arrays, not JavaScript Objects that are already Strings in your PHP. Commented Aug 31, 2015 at 2:22

3 Answers 3

1

Use json_decode to convert 'nutrients' to array.

foreach($array as &$a){
 $a['nutrients'] = json_decode($a['nutrients']); 
}

Then

echo json_encode($array);
Sign up to request clarification or add additional context in comments.

Comments

1

How can I convert an array into a JSON string when it already contains JSON strings, while having no slashes in front of the quotes?

If you want to preserve the JSON values as strings; then, you can't, and you shouldn't be able to!

If your array already contains some JSON values (in which they'll have some quotation marks: ") and you want to encode that array into a JSON string, then the quotation marks must be properly escaped, what you get correctly; otherwise, the entire JSON string will be corrupt because of miss-quotation-mark matches.

That's because the " has a special meaning in JSON, but the \" means the "double quotation mark character" not the special token of "; for example, removing the backslashes from the valid JSON string causes some syntax errors for sure:

$json = '[{"name":"Original","nutrients":"{\"calories\":{\"value\":2500,\"operator\":2},\"protein\":{\"value\":500,\"operator\":1},\"carbs\":{\"value\":200,\"operator\":0},\"fat\":{\"value\":50,\"operator\":0},\"sugar\":{\"value\":1,\"operator\":2}}"},{"name":"Rest","nutrients":"{\"calories\":{\"value\":5000,\"operator\":2},\"sugar\":{\"value\":10,\"operator\":2}}"}]';
$json_noBackslashes = str_replace('\\', '', $json);
$json_decoded = json_decode($json_noBackslashes);
echo json_last_error_msg(); // Syntax error

Comments

0

You should be able to json_decode the json data first, then put it back to the original array. After that, you can encode the whole array again to get the desired output.

foreach ($data as $datum) {
   $data['nutrients'] = json_decode($data['nutrients']);
}

json_encode($data);

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.