NonTry version of Test String for JSON/JSON array
Enhanced version of JSON/JSONarray test function script
by fatmonk
HTML
<div style="border: solid grey 1px;padding: 5px">
<b>Set Up Test</b><hr />
<select id='testSelect' onChange="enableDisableTextArea()">
<option value='{ "a": 1 }'>Test 1: data = '{ "a": 1 }'</option>
<option value='[{ "b": 2 }]'>Test 2: data = '[{ "b": 2 }]'</option>
<option value='[{ 2 }]'>Test 3: data = '[{ 2 }]'</option>
<option value='2'>Test 4: data = '2'</option>
<option value=''>Test 5: date = ''</option>
<option value='null'>Test 6: data is not defined</option>
<option value='user'>User defined test (see below)</option>
</select>
<input type="button" value="Run Test" id="bRunTest" onClick="runTest()"><br />
<textarea disabled style="width: 90%" id="userTestData" placeholder="User test data"></textarea>
</div>
<div style="border: solid grey 1px;padding: 5px">
<b>testForJSON(data) Output</b><hr />
<textarea disabled style="width: 90%" id="resultArea" rows="6"></textarea>
</div>
<div style="border: solid grey 1px;padding: 5px">
<b>testForJSON(data) Return Value</b><hr />
<textarea disabled style="width: 90%" id="returnArea" rows="4"></textarea>
</div>
JavaScript
// This is the implementation of the test for JSON function
// Note that expected results are listed below in the text of the original answer from StackOverflow
function testForJSON(data) {
document.getElementById("resultArea").value = "";
var parseResult;
var isValidJSON = data ? true : false
if (isValidJSON) {
parseResult = JSON.parse(data);
}
if (typeof(parseResult) === 'undefined') {
isValidJSON = false;
}
var isJSONarray = isValidJSON && JSON.parse(data).length ? true : false;
// If is NOT JSON array then the parse will have succeded but will not contain a valid JSON object so clear it
// if (!isJSONarray) { parseResult = undefined }
// Output result in result textarea
document.getElementById("resultArea").value += "Data isValidJSON?: " + isValidJSON + "\n\n";
document.getElementById("resultArea").value += "Data isJSONArray?: " + isJSONarray + "\n\n";
document.getElementById("resultArea").value += "Parse Result: " + parseResult + "\n\n";
document.getElementById("resultArea").value += "Input Test Data : " + data;
return parseResult;
}
// Function below are for interaction of this test page
function runTest() {
document.getElementById("resultArea").value = "";
document.getElementById("returnArea").value = "";
var sel = document.getElementById("testSelect");
if (sel.options[sel.selectedIndex].value == 'null') {
var data;
} else if (sel.options[sel.selectedIndex].value == 'user') {
var data = document.getElementById("userTestData").value;
} else {
var data = sel.options[sel.selectedIndex].value;
}
var processedData = testForJSON(data);
document.getElementById("returnArea").value = "Returned Value = " + processedData + "\n";
document.getElementById("returnArea").value += "Converted to String: " + JSON.stringify(processedData);
}
function enableDisableTextArea() {
var sel = document.getElementById("testSelect");
if (sel.options[sel.selectedIndex].value == 'user') {
...