Phrogz / NeatJSON

Pretty-print your JSON in Ruby, JS, or Lua with more power than JSON.stringify or JSON.pretty_generate
http://phrogz.net/JS/NeatJSON
MIT License
108 stars 19 forks source link

Compact output (some values on one line) #18

Closed gagan-bansal closed 8 years ago

gagan-bansal commented 8 years ago

I am looking for compact output but readable, here is my data:

[
  {
    "id": 1,
    "product": "pumad",
    "price": 48,
    "color": "yellow",
    "available": true,
    "tags": [
      "bravo"
    ],
    "vendor": {
      "name": "Lizzie Chapman",
      "address": {
        "city": "Mumbai"
      }
    }
  },
  {
    "id": 2,
    "product": "rupasge",
    "price": 35,
    "color": "red",
    "available": true,
    "tags": [
      "delta"
    ],
    "vendor": {
      "name": "Ellen Harrison",
      "address": {
        "city": "New York"
      }
    }
  }
]

with following command:

var json = neatJSON(obj,{ short:true, afterComma:1, afterColon1:1, afterColonN:1 });

output is:

[{"id": 1,
  "product": "pumad",
  "price": 48,
  "color": "yellow",
  "available": true,
  "tags": ["bravo"],
  "vendor": {"name": "Lizzie Chapman", "address": {"city": "Mumbai"}}},
 {"id": 2,
  "product": "rupasge",
  "price": 35,
  "color": "red",
  "available": true,
  "tags": ["delta"],
  "vendor": {"name": "Ellen Harrison", "address": {"city": "New York"}}}]

My requirement is to get output wrapped at 80 col:

[{"id": 1, "product": "pumad", "price": 48, "color": "yellow",
  "available": true, "tags": ["bravo"],
  "vendor": {"name": "Lizzie Chapman", "address": {"city": "Mumbai"}}},
 {"id": 2, "product": "rupasge", "price": 35, "color": "red",
  "available": true, "tags": ["delta"],
  "vendor": {"name": "Ellen Harrison", "address": {"city": "New York"}}}]

Would it be possible to get such output. I am looking for this option to show the json data on stackoverflow or github issues or README file.

Phrogz commented 8 years ago

Interesting requirement. I'm not clear on what the wrapping rules would be. If you can make a pull request, or even very clear rules, I'll happily consider merging it.

In your example, you seem to be arbitrarily allowing newlines in some cases, and disallowing them in others. Why not this:

[{"id": 1, "product": "pumad", "price": 48, "color": "yellow", "available": 
true, "tags": ["bravo"], "vendor": {"name": "Lizzie Chapman", "address": 
{"city": "Mumbai"}}}, {"id": 2, "product": "rupasge", "price": 35, "color": 
"red", "available": true, "tags": ["delta"], "vendor": {"name": 
"Ellen Harrison", "address": {"city": "New York"}}}]

Is the rule that Object keys must always stay with their values? If so, then why not:

[{"id": 1, "product": "pumad", "price": 48, "color": "yellow", 
"available": true, "tags": ["bravo"], "vendor": {"name": "Lizzie Chapman",
"address": {"city": "Mumbai"}}}, {"id": 2, "product": "rupasge", "price": 35,
"color": "red", "available": true, "tags": ["delta"],
"vendor": {"name": "Ellen Harrison", "address": {"city": "New York"}}}]

What rule forces a newline after ["bravo"] in your example?

Again, I like the output option, but I don't understand it. If you can put forward unambiguous rules, I will consider coding it. If you provide a pull request implementing something sensible, then I will very likely accept it.

gagan-bansal commented 8 years ago

Thanks for considering my requirement. There could be three rules 1) Each item/object in array at first depth should start with new line 2) Each item/object in array should be wrapped to 80 (wrap option) 3) Key values must be together as you mentioned in in your second example

My requirement is not very critical just to show the json on github issues or README in compact manner so no need to spend mush time if you feel this is not a generic requirement.