0

Hi I've been given the task of formatting JSON using any form of client side. At the moment the JSON is outputted in a standard format like this.

{"Art":[["1","Game Weapons","Weapons","11","GameWeapons_Final_Sheet.jpg"],["2","Violet","Scenery","11","Violet_sheetformat.jpg"]]}

To do this I am using PHP to extract the data and then encode

$jsondata = array();
while($row=mysqli_fetch_row($result))
{
$jsondata['Art'][]=$row;
}
echo json_encode($jsondata);

How do I go about formatting this data before printing and again it has to be client side. Nothing fancy is needed, just simple indenting so it can be read easier. Apologies if I've incorrectly posted or my question doesn't make sense

EDIT

I need my JSON to be indented like this using any client side language

{  
"Art":[  
  [  
     "1",
     "Game Weapons",
     "Weapons",
     "11",
     "GameWeapons_Final_Sheet.jpg"
  ],
  [  
     "2",
     "Violet",
     "Scenery",
     "11",
     "Violet_sheetformat.jpg"
  ]
]
}

EDIT 2

current code

<?php
include ("config/init.php"); 
$connection = mysqli_connect($hostname, $username, $password, $databaseName) or die("you did not    connect");
$query = "SELECT * FROM art";
$result = mysqli_query($connection, $query) or die (mysqli_error($connection));
$jsondata = array();
while($row=mysqli_fetch_row($result))
{
$jsondata['Art'][]=$row;
}
$json = json_encode($jsondata);
?>
<script>
var obj = <?php echo $json; ?>;
var str = JSON.stringify(obj, undefined, 2);
document.write(str);
</script>

All done now, thankyou so much

Calv
  • 133
  • 2
  • 11
  • Its not clear what you want. Could you explain a bit more about your expected output? Any errors? The difference between what you have and what you want? – Vincent Beltman Dec 03 '14 at 12:37
  • What do you mean format JSON? It is what it is. – Antony D'Andrea Dec 03 '14 at 12:44
  • 1
    Why does it have to be formatted client side when the data is stored serverside? – symcbean Dec 03 '14 at 12:46
  • I have no idea myself. I would have used prett print otherwise. Its what ive got to do for uni. My exact task given is output JSON and format appropriatly using client side processing such as jQuery – Calv Dec 03 '14 at 12:50

1 Answers1

1

Try json_encode($jsondata, JSON_PRETTY_PRINT);

For more information check the PHP manual.

If they are so insistant of doing the formatting front end, you could do something like this:

<?php
$jsondata = array();
while($row=mysqli_fetch_row($result))
{
$jsondata['Art'][]=$row;
}
$json = json_encode($jsondata);
?>
<script>
var obj = <?php echo $json; ?>;
var str = JSON.stringify(obj, undefined, 2);
</script>
Antony D'Andrea
  • 991
  • 1
  • 16
  • 35
  • i would use this if i wasnt asked to do it client side – Calv Dec 03 '14 at 12:54
  • May I recomend looking at [this](http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript). – Antony D'Andrea Dec 03 '14 at 12:59
  • Ive had a look at the link but no idea how to implement it with my current code. Sorry about this, im no expert obviously – Calv Dec 03 '14 at 13:09
  • You will need to look into how to use Javascript and HTML. Then use from the answer above `var str = JSON.stringify(obj, undefined, 2);` That is how you would it client side. – Antony D'Andrea Dec 03 '14 at 13:12
  • @adam I updated the answer, but still not sure if this is what they want. It makes no sense since you can do it on the backend! – Antony D'Andrea Dec 03 '14 at 13:39
  • I was just typing something similar. I dont know why my tutor insists on client side either but thankyou. Ive implemented your answer however nothing displays at all now – Calv Dec 03 '14 at 13:59
  • you you will have to do something with str variable. Do some searches in to javascript but this should at least put you in the right track for doing PHP and Java script and HTML on the same page. – Antony D'Andrea Dec 03 '14 at 14:00