I have a JSON object within a single array that was originally a file from which I deleted some fields. Now, I want to modify one of the key values for each entry. Below is an example JSON. I need to iterate through and split the meta_value at
http://www.website/wp-content/uploads/
. Currently, my code is returning every meta_value as undefined instead of splitting the value, which I suspect is due to the loop altering the key value before splitting.
Any assistance would be greatly appreciated.
Here is the code I have created so far that generates the JSON data at the end.
var exclusions = [
"ID", "post_author", "post_date_gmt", "post_excerpt", "comment_status",
"ping_status", "post_password", "to_ping" ,"pinged", "post_modified","post_name",
"post_modified_gmt", "post_content_filtered", "guid", "menu_order", "post_mime_type",
"comment_count", "meta_id", "post_id", "post_type", "post_status"
];
var a = JSON.parse(fs.readFileSync('final.json'));
a.forEach(obj=>{
exclusions.forEach(excl=>{
if(obj[excl] || obj[excl] === ""){
delete obj[excl];
}
if(obj["meta_value"] !== undefined){
let objTest = obj["meta_value"].split('http://www.fsd.ca/wp-content/uploads/')[1];
obj["meta_value"] = objTest;
}
});
});
console.log(a);
JSON after initial exclusions
[
{
post_date: '2012-02-16 23:37:22',
post_content: `Today we worked at literacy centres. We are writing our own Three Bears story. We are reading with Mrs.Kitson, writing in our life books, working in our printing books and working on making words on the iPads. We use the apps Pocket phonics, magnet board and Montessori crosswords. We went to the library. In the afternoon we went to the gym and watched a play by Quest Theatre. It was called <span style="text-decoration: underline;">For</span> <span style="text-decoration: underline;">Art's Sake.</span> They told us that we are all artists and that we should use our imagination. We did zumba. We danced to The chihuahua song. We went to the DPA room and played different tag games. Our hearts worked hard. We had a fun day!`,
post_title: 'Hometime',
meta_value: 'http://www.fsd.ca/wp-content/uploads/2012/02/SN850631.jpg'
},
...
]
- List item
JSON after changing the meta_value key value, where every value is undefined
[
{
post_date: '2012-02-16 23:37:22',
post_content: `Today we worked at literacy centres. We are writing our own Three Bears story. We are reading with Mrs.Kitson, writing in our life books, working in our printing books and working on making words on the iPads. We use the apps Pocket phonics, magnet board and Montessori crosswords. We went to the library. In the afternoon we went to the gym and watched a play by Quest Theatre. It was called <span style="text-decoration: underline;">For</span> <span style="text-decoration: underline;">Art's Sake.</span> They told us that we are all artists and that we should use our imagination. We did zumba. We danced to The chihuahua song. We went to the DPA room and played different tag games. Our hearts worked hard. We had a fun day!`,
post_title: 'Hometime',
meta_value: undefined
},
...
]