0

I have a question about json formatting. I have a requirement to produce the following json for a jquery plugin named DataTables...

 {
  "aaData": [{
      "name": "Zane Music",
      "address": "162 Castle Hill",
      "town": "Berkshire"
  }, {
      "name": "Zander Exports",
      "address": "34 Sapcote Trading Centre",
      "town": "London"
  }]
}

To try and achieve this I am using org.json.simple and have managed to produce the following...

 {
 "aaData": [{
     "name": "Zane Music",
     "address": "162 Castle Hill",
     "town": "Berkshire"
 }, {
     "name": "Zander Exports",
     "address": "34 Sapcote Trading Centre",
     "town": "London"
 }]
}

i.e. they are the same but the second listing is missing line breaks and is not properly formatted.

My question is does this matter for the consumer and is it good practice to have the json nicely formatted? Also is there a way to make it nicely formatted?

With json simple you specify the json by adding a method in the domain object like this...

public String toJSONString() {
    JSONObject obj = new JSONObject();
    obj.put("location", _location);
    obj.put("locationName", _locationName);
    obj.put("region", _region);
    return obj.toString();
}

So I guess I could add line breaks where necessary.

Is that the right way to do it / is it necessary?

thanks

xxbinxx
  • 1,527
  • 11
  • 18
Richie
  • 4,989
  • 24
  • 90
  • 177

1 Answers1

1

My question is does this matter for the consumer and is it good practice to have the json nicely formatted?

The JSON specification states

Insignificant whitespace is allowed before or after any of the six structural characters.

So, no, whitespace has not significance. A proper JSON consumer will not see the difference. Formatting is just for the human eye.

Also is there a way to make it nicely formatted?

I don't know for org.json, but some JSON parsing libraries have a utility or configuration option for that. Consider using Gson or Jackson with pretty printing options. Here are some more options.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • thank you! That helped me a lot. Unfortunately I can't use gson jackson and a lot of other nice stuff bc the server I am deploying to is still on java 1.4.2! – Richie Sep 03 '14 at 06:16
  • @Richie In the last link I posted I think there's a solution with `org.json`. Might want to try that. – Sotirios Delimanolis Sep 03 '14 at 06:17
  • I got excited for a minute! But then I realised that org.json is different to org.json.simple. Almost! It's ok. I feel comfortable now knowing it is only an aesthetic issue. – Richie Sep 03 '14 at 06:26
  • @Richie Ah, sorry. In any case, if the JSON is only meant for the plugin, pretty printing is not necessary. For your debugging purposes, consider using a tool like [jsonlint](http://www.jsonlint.com). – Sotirios Delimanolis Sep 03 '14 at 06:27
  • wow! wow! wow! Excellent tool. I love it. Thanks. Did I mention wow? – Richie Sep 03 '14 at 06:29