JSFiddle - React, Tailwind, and code Playground
JavaScript
var response1 = JSON.parse("{ \"renditions\": { \"live\": \"true\", \" type\": \"default\", \"rendition\": { \"name\": \"Live \", \"url\": \"http: //mysite\" } } }");
var response2 = JSON.parse("{ \"renditions\": { \"live\": \"true\", \" type\": \"default\", \"rendition\":[ { \"name\": \"Live \", \"url\": \"http: //mysite\" } ] } }");
function modify(response) {
if (Object.prototype.toString.call( response.renditions.rendition ) !== '[object Array]') {
response.renditions.rendition = [response.renditions.rendition];
}
return response;
}
console.log("started as object: " + JSON.stringify(modify(response1)));
console.log("started as array: " + JSON.stringify(modify(response2)));