I'm currently in the process of formatting an array into a JSON object for API submission.
Struggling to find the right method to transform my array into the desired structure.
This is what my array looks like:
data: [
["Lisa", "Heinz", "1993-04-15" ],
["Bob", "Dylan", "1998-09-12"],
["Cabbage", "Man", "1990-01-11"],
["", "", ""]
]
What I want it to look like as a JSON object:
{person:[{"name":"Lisa","last_name":"Heinz","dob":"1993-04-15"},{"name":"Bob","last_name":"Dylan","dob":"1998-09-12"},{"name":"Cabbage","last_name":"Man","dob":"1990-01-11"},{"name":"","last_name":"","dob":""}],"object_id":259,"test":"bob","attribute":"bob123"}
Here's my current approach:
let json = {}
for (let person of formData) {
const identifier = `person${formData.indexOf(person)}`;
json[identifier] = {
name: person[0],
last_name: person[1],
dob: person[2]
}
}
json.object_id = "259";
json.wp_test = "bob";
json.attribute = "bob123";
The output resembles this:
{"person0":{"name":"Lisa","last_name":"Heinz","dob":"1993-04-15"},"person1":{"name":"Bob","last_name":"Dylan","dob":"1998-09-12"},"person2":{"name":"Cabbage","last_name":"Man","dob":"1990-01-11"},"person3":{"name":"","last_name":"","dob":""},"object_id":259,"wp_test":"bob","attribute":"bob123"}
Experimented with various approaches but haven't found the ideal solution yet - would appreciate a straightforward method to achieve the desired shape.