Mind you this uses lodash
to detect Arrays and Objects, here's another method that will keep the "leaf" objects compact on one line:
_.jsonPretty = function(obj, indent) {
if(!indent) indent = 2;
return JSON.stringify(obj, function(k,v) {
//Check if this is a leaf-object with no child Arrays or Objects:
for(var p in v) {
if(_.isArray(v[p]) || _.isObject(v[p])) {
return v;
}
}
return JSON.stringify(v);
//Cleanup the escaped strings mess the above generated:
}, indent).replace(/\\/g, '')
.replace(/\"\[/g, '[')
.replace(/\]\"/g,']')
.replace(/\"\{/g, '{')
.replace(/\}\"/g,'}');
};
And just use it like this:
_.jsonPretty(yourObjectToStringify);
Here's an example before...
{
"type": "light-item",
"name": "Waiting",
"ringSeqLooping": true,
"ringSeqHoldLast": false,
"ringSteps": [
{
"type": "light-step",
"time": 1,
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
},
{
"type": "light-step",
"time": "0.5",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
}
],
"stripSeqLooping": true,
"stripSeqHoldLast": false,
"stripSteps": [
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "FadeOn",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
},
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": 1,
"lights": [
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "FadeOff",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
},
{
"state": "Off",
"color": "#fff"
}
]
}
]
}
... and after:
{
"type": "light-item",
"name": "Waiting",
"ringSeqLooping": "true",
"ringSeqHoldLast": "false",
"ringSteps": [
{
"type": "light-step",
"time": "1",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"FadeOn","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
},
{
"type": "light-step",
"time": "0.5",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"FadeOff","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
}
],
"stripSeqLooping": "true",
"stripSeqHoldLast": "false",
"stripSteps": [
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"FadeOn","color":"#fff"},
{"state":"FadeOn","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
},
{
"type": "light-step",
"time": "2",
"audioClipName": "Off",
"audioVolume": "1",
"lights": [
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"FadeOff","color":"#fff"},
{"state":"FadeOff","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"},
{"state":"Off","color":"#fff"}
]
}
]
}