Basically I want to convert an Excel Data into a complex JSON. So far, I've converted the excel data into array of rows like below. First array will always be the headings of the table.
const row = [["rule", "key_phrase", "doc_type", "doc_priority", "section_title"],
["approval related", "approved", "assessment report", 1, "4. Recommendations, outcome"],
["prime designation", "prime,prime designation", "assessment-report", 1, "1. Background information on the procedure, 1.1. Submission of the dossier"],
["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "5.1 Pharmaco dymnamic properties, Clinical Efficacy"],
["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "CLINICAL/STATISTICAL/PHARMACOVIGILANCE, Clinical Program"]]
And now I want to convert that array into a JSON. Every comma ,
separated values should go into the same key. For eg. key_phrase: ["prime", "prime designation"] .
So from the above excel data, the first JSON object should be something like this:
{
"biz_rules": [
{
"rule": "approval related",
"key_phrase": [
"approved"
],
"doc": [
{
"type": "assessment-report",
"priority": 1,
"sections": [
{
"title_keywords": [
"4. Recommendations",
"outcome"
],
"content_keywords": [
"marketing authorization"
]
}
]
}
]
},
{
"rule": "prime designation",
"key_phrase": [
"prime",
"prime designation"
],
"doc": [
{
"type": "assessment-report",
"priority": 1,
"sections": [
{
"title_keywords": [
"1. Background information on the procedure",
"1.1. Submission of the dossier"
],
"content_keywords": [
"prime"
]
}
]
}
]
},
{
"rule": "single-arm trials",
"key_phrase": [
"single-arm",
"single arm",
"single",
"arm"
],
"doc": [
{
"type": "NDA",
"priority": 2,
"sections": [
{
"title_keywords": [
"5.1 Pharmacodynamic properties",
"Clinical efficacy"
],
"content_keywords": [
"single-arm trials"
]
},
{
"title_keywords": [
"CLINICAL/STATISTICAL/PHARMACOVIGILANCE",
"Clinical Program"
],
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "assessment-report",
"priority": 1,
"sections": [
{
"title_keywords": [
"5. Clinical efficacy ",
"2.5.2. Main studies"
],
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "Clinical trials",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "press-release",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "press-releases",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
},
{
"type": "media-releasess",
"priority": 2,
"sections": [
{
"content_keywords": [
"single-arm trials"
]
}
]
}
]
}
]}
Ignoring content_keywords . The rules are uniquely divided as per sections. So every title_keywords inside the sections would be separate rule in excel.
Any help would be much appreciated. Thanks in Advance.
You can start with
const valueToCsv = text => (text.indexOf(', ') > 0) ? text.split(', ') : text; const newArr = arr.slice(1).map(row => ({ rule: valueToCsv(row[0]), key_phrase: valueToCsv(row[1]), doc: [{ type: valueToCsv(row[2]), priority: parseInt(row[3]), sections: [ { title_keywords: valueToCsv(row[4]) } ] }] })); console.log(JSON.stringify(newArr));
this code.
You can try with following:
const data = [["rule", "key_phrase", "doc_type", "doc_priority", "section_title"], ["approval related", "approved", "assessment report", 1, "4. Recommendations, outcome"], ["prime designation", "prime,prime designation", "assessment-report", 1, "1. Background information on the procedure, 1.1. Submission of the dossier"], ["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "5.1 Pharmaco dymnamic properties, Clinical Efficacy"], ["single-arm trials", "single-arm, single arm, single, arm", "NDA", 2, "CLINICAL/STATISTICAL/PHARMACOVIGILANCE, Clinical Program"]] data.shift() const obj = data.map(e => { return { rule: e[0], key_phrase: e[1], doc: { doc_type: e[2], doc_priority: e[3], }, section_title: e[4], } })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.