I have an array in the database and it looks like this: {"hello":"world", "Test":["hello"]}, which is perfect for JSON.stringify, but when I select it from the database like this:
$metadata = $this->repository->getMetadata($id);
$data = json_encode($metadata);
return $this->render('AcmeQuotesBundle:Home:metadata.html.twig', array('data' => $data));
and put this in the template:
{% block body %}
<script>
var obj = {{ data|raw }}
document.body.innerHTML = "";
document.body.appendChild(document.createTextNode(JSON.stringify(obj, null, 4)));
</script>
{% endblock %}
I get this output:
[
{
"quoteMetadata": "{\"hello\":\"world\", \"Test\":[\"hello\"]}"
}
]
which is not what I want. All I want is the value of obj to be the native string - {"hello":"world", "Test":["hello"]} without "quoteMetadata":, without the quotation marks and without the "\" around the words. I tried using implode(), but I get notice:
Notice: Array to string conversion in C:\xampp\htdocs...
I'm using Symfony2, Twig and Doctrine2 and I'm doing all this because I want the string from the database to be shown in a easy for reading way like here - http://jsfiddle.net/AndyE/HZPVL/ If you have any ideas how to fix this or how to make it in another way, plase share!
EDIT
When I put var_dump($matadata) after $metadata = $this->repository->getMetadata($id);, I get this:
array(1) { [0]=> array(1) { ["quoteMetadata"]=> string(35) "{"hello":"world", "Test":["hello"]}" } }
json_encode-ing something that's alreadyjson_encoded. Can you do avar_dump($quoteMetadata);after the first line and show us what it looks like?