I want to parse JSONs files, which look like the data file in the function below.
I can extract all the data I need for my report from data except the options array ids
so in the question Sector
of the json file data, it has the options array
"options": {
"62f92fab79ac81d933765bd0bbc4a1f5ea26cb3a088bcb4e6e": {
"index": 0,
"value": "Bob",
"label": "Bob",
"count": 1
},
"2fe91aa3567c0d04c521dcd2fc7e40d7622bb8c3f594d503da": {
"index": 1,
"value": "Student",
"label": "Student",
"count": 1
},
"c59ea1159f33b91a7f6edc6925be5e373fc543e4": {
"index": 5,
"value": "AAA",
"label": "AAA"
}
}
I need to extract
62f92fab79ac81d933765bd0bbc4a1f5ea26cb3a088bcb4e6e
2fe91aa3567c0d04c521dcd2fc7e40d7622bb8c3f594d503da
c59ea1159f33b91a7f6edc6925be5e373fc543e4
and added it to my output
The output I have
c3c6f410f58e5836431b473ebcf134756232d04f2bf35edff8
Sector
Bob
Student
AAA
f794c6a52e793ee6f5c42cd5df6b4435236e3495e951709485
Question AAA
The output I need
c3c6f410f58e5836431b473ebcf134756232d04f2bf35edff8
Sector
62f92fab79ac81d933765bd0bbc4a1f5ea26cb3a088bcb4e6e
Bob
2fe91aa3567c0d04c521dcd2fc7e40d7622bb8c3f594d503da
Student
c59ea1159f33b91a7f6edc6925be5e373fc543e4
AAA
f794c6a52e793ee6f5c42cd5df6b4435236e3495e951709485
Question AAA
Thanks
function ParseJSON() {
let data = [
{
"id": "c3c6f410f58e5836431b473ebcf134756232d04f2bf35edff8",
"component": "checkbox",
"customFields": [
],
"index": 0,
"label": "Sector",
"options": {
"62f92fab79ac81d933765bd0bbc4a1f5ea26cb3a088bcb4e6e": {
"index": 0,
"value": "Bob",
"label": "Bob",
"count": 1
},
"2fe91aa3567c0d04c521dcd2fc7e40d7622bb8c3f594d503da": {
"index": 1,
"value": "Student",
"label": "Student",
"count": 1
},
"c59ea1159f33b91a7f6edc6925be5e373fc543e4": {
"index": 5,
"value": "AAA",
"label": "AAA"
}
},
"required": false,
"validation": "/.*/",
"imported": false
},
{
"id": "f794c6a52e793ee6f5c42cd5df6b4435236e3495e951709485",
"component": "textInput",
"customFields": [
],
"index": 1,
"label": "Question AAA",
"options": {
},
"required": false,
"validation": "/.*/",
"imported": false
}
];
let result = "";
data.forEach(({id, label, content, component, options}) => {
if(component!=="htmlelement"){
//filter out HTML componets
content = label==="HTML" ? "":content;
label= label==="HTML" ? "":label;
content = content ? 'n' + content:"";
component = component==='checkbox' ? "select_boxes":component;
component = component==='textInput' ? "text input":component;
component = component==='textArea' ? "text area":component;
component = component==='url' ? "URL":component;
component = component!=='htmlelement' ? component+'n':"";
if(id + component + label + content!==""){
result += id + 'n' + label + content + 'n'+ parseOBJ(options);
//result += console.log(component + label + content + 'n'+ parseOBJ(options));
result += 'n'
}
result=result.replace(/[rn]{2,}/g, "nn");
}
});
console.log(result)
}
function parseOBJ(obj) {
let R =""
for (let [key, value] of Object.entries(obj)) {
R += value.value + 'n';
};
return R
}