3

Right now I am getting my JSON url response as I would like to pretty print this URL

[{"chat":"ed test","displayname":"Ed Student","created_at":"2014-03-29 21:42:35"},
{"chat":"test from sue","displayname":"Sue Student","created_at":"2014-03-29 21:42:25"},{"chat":"Test from instructor","displayname":"Jane Instructor","created_at":"2014-03-29 21:42:18"}]

but I would like to get it to look like this:

[

   {

     "chat":"ed test",
     "displayname":"Ed Student",
      "created_at":"2014-03-29 21:42:35"},
    {
      "chat":"test from sue",
      "displayname":"Sue Student",
    }
]

and so on.

My code is following:

$.ajax({

        type: "POST",
        url: "chatlist.php?PHPSESSID=9c2b21f6309fd0cb8c157b54eafb7381",
        cache: true,
        async: true,
        dataType:"json",
        data: JSON.stringify(messages, null, 4),
    success: function(data){ 
            $("#messages").empty();
            for (var i = 0; i < data.length; i++) {
                entry = data[i];
                $("#messages").append("<p>"+htmlentities(entry.chat)+'<br/>&nbsp;&nbsp;'
                        +htmlentities(entry.displayname)+' '+htmlentities(entry.created_at)+"</p>\n");
                    console.log(JSON.stringify(data[i]));
            }
            OLD_TIMEOUT = setTimeout('messages()', 4000);
        }
  });

Is it possible to format the response URL that I am getting with this $.ajax call?

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
chatycat99
  • 109
  • 1
  • 10
  • 1
    @arunraj - I have rolled this back because you tidied up the OP's JSON source and rendered the question meaningless. –  Mar 30 '14 at 06:01
  • Well presumably, it is your PHP will generating the JSON. You should show that. Also, is the only difference in JSON that some objects don't have dates? You are not really clear on describing the difference. – Mike Brant Mar 30 '14 at 06:02
  • your current data is a superset of your optimal data, which means that you could (depending on your implementation) just access the data you want and disregard the redundant data. What is the problem? – twinlakes Mar 30 '14 at 06:05
  • No difference in JSON only formatting issues. Could this be the issue in my PHP echo(json_encode($messages)) – chatycat99 Mar 30 '14 at 06:05
  • 2
    Ok after rolling back the edit... Are you just wanting your JSON to be pretty printed? If so, why do you care? The JSON is not meant to be read by a person, but by your code. If you want this just for testing, than any number of RESTful testing tools, will automatically display pretty print for you. – Mike Brant Mar 30 '14 at 06:05
  • possible duplicate of [How can I beautify JSON programmatically?](http://stackoverflow.com/questions/2614862/how-can-i-beautify-json-programmatically) – Zafar Mar 30 '14 at 06:13
  • 1
    `async:true` - no need to specify that. Also, it causes unnecessary shivers in people who know that `async:false` is bad. – John Dvorak Mar 30 '14 at 06:50

3 Answers3

2

I think you are looking for,

JSON.stringify(value[, replacer [, space]])

Like,

var data = JSON.stringify(data, null, "\t"); // Using a tab character mimics standard pretty-print appearance

console.log(data);

Check

DEMO

Read:

JSON.stringify

Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79
1

In my PHP code I had

echo(json_encode($messages));

When I should have had

echo(json_encode($messages, JSON_PRETTY_PRINT));

Which was printing out my JSON.

chatycat99
  • 109
  • 1
  • 10
-1

jq is a command line json processor. you can use jq also for formatting json. when you saved your ajax call responses as json files then jq is the best way to format it on command line.

example:

$ echo '{ "foo": "lorem", "bar": "ipsum" }' | jq .
{
  "bar": "ipsum",
  "foo": "lorem"
} 

see jq homepage with tutorials: http://stedolan.github.io/jq/

see also the similar SO question: How can I pretty-print JSON in (unix) shell script?

Community
  • 1
  • 1
muescha
  • 1,544
  • 2
  • 12
  • 22