Here is our approach: We start by importing a json file and then proceed to look for keys that contain "answer_comments" in the record, as this key is present in all the questions.
Next, we use regex to extract the titles of the questions.
Once we have the question titles, we iterate through them to retrieve the corresponding values.
const data = require("./data.json");
const record = data["ex:recordCollection"]["ex:record"];
const questions = Object.keys(record).filter((a) =>
a.toLowerCase().includes("answer_comments")
);
const result = questions.map((q) => {
const title: string = q.match(/(?<=ex:)(.*)(?=__)/gm)[0];
return {
question: title,
answer: record[`ex:${title}`]["ex:Item"]["_text"],
answer_comment: !record[`ex:${title}__Answer_Comments`] && "",
implementation: record[`ex:${title}__Implementation`]
? record[`ex:${title}__Implementation`]["ex:Item"]["_text"]
: "",
implementation_comments:
!record[`ex:${title}__Implementation_Comments`] && "",
};
});
console.log(result);