I'm flattening a JSON structure where objects have their interaction type appended at the end, for use in an HTML/PDF generated file. I have variable names for Checkboxes, Text inputs, TextCheck is a combined checkbox/input that needs to be broken apart for my logic. Lastly, I have a Radio Index and Radio Label ojbect in which Radio Label is an array of options to choose from, and Radio Index is the index which is filled, or null for unfilled. However, I have variables in the HTML file which need an associated JSON key so I must generate these.
Kind of new to the professional world and javascript so I'd like to see where I can beautify code, work out poor blocks, replace unsound variable names, etc. My coding style given is very relaxed, but mostly avoiding Else If, instead utilizing early exit, as well as having bracket openings on the same line.
JSON Snippet:
"billingSelfPayObject":
{
"Amount_Text":null,
"Credit Card_RadioIndex":null,
"Credit Card_RadioLabels":["MasterCard", "Visa", "Discover", "American Express"],
},
"patientQuestionnaire":
{
"generalQuestions":
{
"indication":
{
"Diagnostic_Checkbox":null,
"Family history_Checkbox":null,
"Known variant_Checkbox":null,
"Presymptomatic_Checkbox":null,
"Other (Specify)_TextCheck":null
},
"ICD-10 Code(s)_Text":null,
"Clinical diagnosis_Text":null
}, ...
Code:
function isDigit(num)
{
if(typeof(num)==='number' || typeof(num)==='string')
{
return !isNaN(parseInt(num, 10));
}
return false;
}
function mergeObjects(obj1, obj2)
{
for(var key in obj2)
{
if(!(key in obj1))
{
obj1[key] = obj2[key];
}
}
}
function processKVFromObject(value, currName)
{
var result = {};
var currName = currName || "";
if(typeof(value)==='string' || typeof(value)==='boolean' || typeof(value)==='number' || value==null)
{
var splitName = currName.split("_");
var identifier = splitName.pop();
console.log(identifier);
if(identifier == "TextCheck")
{
var boxFlag;
value ? boxFlag = 'Y' : boxFlag = null;
result[splitName.join('_').toUpperCase() + '_CHECKBOX'] = boxFlag;
result[splitName.join('_').toUpperCase() + '_TEXT' ] = value;
return result;
}
if(identifier == "RadioIndex")
{
if(value)
{
result[splitName.join('_').toUpperCase() + '_RADIOLABELS_' + value] = true;
}
}
if(isDigit(identifier)) //Check if last element of splitName is a radio button label
{
console.log("got a digit!");
result[currName.toUpperCase()] = null; //If so, use it to identify radio buttons, but throw out the description
return result;
}
result[currName.toUpperCase()] = value;
return result;
}
if(currName != "")
{
currName = currName + '_';
}
if(value instanceof Array)
{
for(var i = 0; i < value.length; i++)
{
mergeObjects(result, processKVFromObject(value[i], currName + i));
}
}
else
{
for(key in value)
{
mergeObjects(result, processKVFromObject(value[key], currName + key));
}
}
return result;
}
function flattenJSON(json)
{
var keyValues = {};
try
{
keyValues = processKVFromObject(json);
}
catch(err)
{
console.log("\nERROR in flattenJSON: " + err);
}
return keyValues;
}
Output:
"BILLINGSELFPAYOBJECT_AMMOUNT_TEXT": null,
"BILLINGSELFPAYOBJECT_CREDIT CARD_RADIOINDEX": null,
"BILLINGSELFPAYOBJECT_CREDIT CARD_RADIOLABELS_0": null,
"BILLINGSELFPAYOBJECT_CREDIT CARD_RADIOLABELS_1": null,
"BILLINGSELFPAYOBJECT_CREDIT CARD_RADIOLABELS_2": null,
"BILLINGSELFPAYOBJECT_CREDIT CARD_RADIOLABELS_3": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_INDICATION_DIAGNOSTIC_CHECKBOX": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_INDICATION_FAMILY HISTORY_CHECKBOX": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_INDICATION_KNOWN VARIANT_CHECKBOX": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_INDICATION_PRESYMPTOMATIC_CHECKBOX": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_INDICATION_OTHER (SPECIFY)_CHECKBOX": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_INDICATION_OTHER (SPECIFY)_TEXT": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_ICD-10 CODE(S)_TEXT": null,
"PATIENTQUESTIONNAIRE_GENERALQUESTIONS_CLINICAL DIAGNOSIS_TEXT": null,