0

I pulled some data from my database. Is working perfectly. But I want to remove the double quotes in the JSON. Here are my codes;

$sql = "SELECT id, instructions, quiz_question, correct, wrong, wrong1, wrong2 FROM student_quiz WHERE subject = 'SOCIAL STUDIES' AND type = 'challenge'";

$results = $pdo->query($sql);
$results->setFetchMode(PDO::FETCH_ASSOC);

$json = [];

while($row = $results->fetch()) {
    $choices = [
        $row['correct'],
        $row['wrong'],
        $row['wrong1'],
        $row['wrong2'],
    ];

    // shuffle the current choices so the 1st item is not always obviously correct
    shuffle($choices);

    $json[] = [
        'question' => $row['quiz_question'],
        'choices' => $choices,
        'correctAnswer' => $row['correct'],
    ];
}

echo json_encode($json);

Is echoing a data like this;

{"question":"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>","choices":["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],"correctAnswer":"Kwame Nkrumah"}

But I want it like this:

{question:"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>",choices :["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],correctAnswer :"Kwame Nkrumah"}
Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
Robee
  • 23
  • 1
  • 5
  • 6
    JSON requires that property names are quoted with double-quotes. Why do you want to change that? What are you trying to achieve? – Pointy Sep 10 '18 at 20:34
  • What he said, and you don't want it like that because you wont be able to decode it using `json_decode`, in fact I wrote a parser for another question a while back because they had json without the quotes that they couldn't parse using the built in function... just saying. – ArtisticPhoenix Sep 10 '18 at 20:36
  • 2
    The format you want is called "JSOL" (JavaScript object literals). As mentioned already there's few parsers and generators for that in PHP land. And should generally be avoided unless there's a better reason than "I want". – mario Sep 10 '18 at 20:39
  • I am using the data in a javascript function which does not require the double quote – Robee Sep 10 '18 at 20:40
  • 3
    @Robee the double quotes are perfectly fine for JavaScript object literal notation. You don't need to get rid of them. – Pointy Sep 10 '18 at 20:41
  • 1
    Javascript should still work with the quotes though. JSON.parse() – Chris Sep 10 '18 at 20:41
  • Pls How can you get the index of the correctAnswer? – Robee Sep 11 '18 at 00:23
  • Check this please https://stackoverflow.com/questions/13523729/how-to-json-encode-php-array-but-the-keys-without-quotes – Anjana Silva Sep 11 '18 at 11:22

2 Answers2

2

The output PHP gave is correct, if you need this output:

{question:"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>",choices :["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],correctAnswer :"Kwame Nkrumah"}

in javascript, try using..

var json = JSON.parse(response);

in php try using

$json = json_decode($json);

Hope this helps.

Ifeanyi Amadi
  • 776
  • 5
  • 10
0

you could try to encode then decode it like this

$encodeJson = json_encode($json);

print_r(json_decode($encodeJson, true));

if you would like each offset then you can print it like this and no quotes will be added:

$decodeJson = json_decode($encodeJson, true);
print $decodeJson['question'];

EDIT: below code is comment answer:

$data = array(
"question" => "Who said this statement Ghana your beloved country is free for ever",
"choices" => array("Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"),
"correctAnswer" => "Kwame Nkrumah"
);
$jsonEncode = json_encode($data);
$jsDecode = json_decode($jsonEncode, true);

// a correct json encoded array would output something like this:

{
"question":"Who said this statement Ghana your beloved country is free for ever ?",
"choices":[
    "Jack Chan",
    "Donald Trump",
    "Ato Ahoi",
    "Kwame Nkrumah"
],
"correctAnswer":"Kwame Nkrumah"
}

//to select correct answer
print $jsDecode['correctAnswer'];
JamesBond
  • 312
  • 2
  • 17