0

I have a json object inside a single array, that was an original file that I have deleted some fields, now i want to mutate one of the key values for each entry. Here is some example Json. I Want to loop through and split the meta_value at http://www.website/wp-content/uploads/ right now my code is returning every meta_value as undefined instead of splitting the value, I think it has to do with the loop changing the key value and trying to split it after.

Any help would be greatly appreciated

This is the code I have so far that generates the json data at the very 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
  1. List item

.ca/wp-content/uploads/2012/02/SN850631.jpg' }, { post_date: '2012-02-21 20:39:19', post_content: 'Today we started making our castles. We painted them colourful tissue paper. We are writing our own fairy tales. We read The Princess Frog Fairy Tale. We are making clay dragons and sewing puppets. We went to the gym and skipped. We wrote in our life books an practiced our printing. We had a fun day!', post_title: 'Creating Castles', meta_value: 'http://www.fd.ca/wp-content/uploads/2012/02/SN850649.jpg' }, { post_date: '2012-02-23 21:30:55', post_content: 'We talked about the letter E. E is a vowel. There is a vowel in every word. We did our E sheets. We went skating. We had hot chocolate after skating. We worked at new literacy centers. We are doing hard work. We went to Music. We played the bumble bee game. We went to the DPA room to play tag and dance. We went to centers and to the library. We had a fun day!', post_title: 'We Have New Pillows', meta_value: 'http://www.fds.ca/wp-content/uploads/2012/02/SN850660.jpg' }, { post_date: '2012-02-24 19:06:39', post_content: 'Today it was Hawaii Day. We wore shorts and summer clothes. We made a Chicka Chicka Boom Boom picture. We made leis. We ate batter dipped pineapples and ate mango, pineapple, papaya, starfruit, dragon fruit and coconut. We tried coconut water. We did the limbo and danced Hawaii style. We sat on our towels and played a math game where we guessed how many candies were under the cup. We wrote in our life books and we went to the big park. We had a fun, fun day!', post_title: 'Hawaii Day', meta_value: 'http://www.ff.ca/wp-content/uploads/2012/02/SN850665.jpg' }, ... 654 more items ]

Json after meta_value key value change, 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
  },
  {
    post_date: '2012-02-21 20:39:19',
    post_content: 'Today we started making our castles. We painted them colourful tissue paper. We are writing our own fairy tales. We read The Princess Frog Fairy Tale. We are making clay dragons and sewing puppets. We went to the gym and skipped. We wrote in our life books an practiced our printing. We had a fun day!',
    post_title: 'Creating Castles',
    meta_value: undefined
  },
  {
    post_date: '2012-02-23 21:30:55',
    post_content: 'We talked about the letter E. E is a vowel. There is a vowel in every word. We did our E sheets. We went skating. We had hot chocolate after skating. We worked at new literacy centers. We are doing hard work. We went to Music. We played the bumble bee game. We went to the DPA room to play tag and dance. We went to centers and to the library. We had a fun day!',
    post_title: 'We Have New Pillows',
    meta_value: undefined
  },
  {
    post_date: '2012-02-24 19:06:39',
    post_content: 'Today it was Hawaii Day. We wore shorts and summer clothes. We made a Chicka Chicka Boom Boom picture. We made leis. We ate batter dipped pineapples and ate mango, pineapple, papaya, starfruit, dragon fruit and coconut. We tried coconut water. We did the limbo and danced Hawaii style. We sat on our towels and played a math game where we guessed how many candies were under the cup. We wrote in our life books and we went to the big park. We had a fun, fun day!',
    post_title: 'Hawaii Day',
    meta_value: undefined
  },
  ... 654 more items
]
17
  • 1
    How did you put a JSON object into an array? Commented Jan 23, 2020 at 6:08
  • I exported it from wordpress a long time ago, and I no longer have access to that wordpress database. Commented Jan 23, 2020 at 6:09
  • I doubt that, please read the linked article, JSON is a textual data exchange format, it doesn't implement any objects. Commented Jan 23, 2020 at 6:10
  • well I don't know what I did it was a while ago, it was csv, and then I think i converted to json, I don't know. What exactly is wront with what I have, can I not just change it so it's formatte correctly Commented Jan 23, 2020 at 6:12
  • Just read the MDN article ... Commented Jan 23, 2020 at 6:13

2 Answers 2

1

The problem is that you have the code to change obj["meta_value"] inside the exclusions.forEach() loop. So it runs multiple times for the same object. The first time, it replaces 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850631.jpg' with '2012/02/SN850631.jpg'. The second time, it doesn't find the delimiter 'http://www.mrskitson.ca/wp-content/' in the meta value, so split() returns an array with only 1 element, and [1] is undefined.

Take it out of the inner loop and it should work correctly.

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 = [
  {
    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.mrskitson.ca/wp-content/uploads/2012/02/SN850631.jpg'
  },
  {
    post_date: '2012-02-21 20:39:19',
    post_content: 'Today we started making our castles. We painted them colourful tissue paper. We are writing our own fairy tales. We read The Princess Frog Fairy Tale. We are making clay dragons and sewing puppets. We went to the gym and skipped. We wrote in our life books an practiced our printing. We had a fun day!',
    post_title: 'Creating Castles',
    meta_value: 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850649.jpg'
  },
  {
    post_date: '2012-02-23 21:30:55',
    post_content: 'We talked about the letter E. E is a vowel. There is a vowel in every word. We did our E sheets. We went skating. We had hot chocolate after skating. We worked at new literacy centers. We are doing hard work. We went to Music. We played the bumble bee game. We went to the DPA room to play tag and dance. We went to centers and to the library. We had a fun day!',
    post_title: 'We Have New Pillows',
    meta_value: 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850660.jpg'
  },
  {
    post_date: '2012-02-24 19:06:39',
    post_content: 'Today it was Hawaii Day. We wore shorts and summer clothes. We made a Chicka Chicka Boom Boom picture. We made leis. We ate batter dipped pineapples and ate mango, pineapple, papaya, starfruit, dragon fruit and coconut. We tried coconut water. We did the limbo and danced Hawaii style. We sat on our towels and played a math game where we guessed how many candies were under the cup. We wrote in our life books and we went to the big park. We had a fun, fun day!',
    post_title: 'Hawaii Day',
    meta_value: 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850665.jpg'
  },
];

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.mrskitson.ca/wp-content/uploads/')[1];
    obj["meta_value"] = objTest
  }
});

console.log(a);

Sign up to request clarification or add additional context in comments.

Comments

1

At below line let objTest = obj["meta_value"].split('http://www.mrskitson.ca/wp-content/uploads/')[1]; When you use split() it returns you empty array and [] of 0th element is not exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.