How to write JSON data and merge them together


  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
 

{
    "userId": 1,
    "id": 3,
    "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
    "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
  }

I want to append both objects in a JSON file one by one and the format should be like this

[
 {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
{
    "userId": 1,
    "id": 3,
    "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
    "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
  }
]

How can I get this functionality? Thanks!

1 Like

Are you running this program on Node or the browser?

In Node you can call require with JSON files so:

const jsonOne = require('path/to/one.json');
const jsonTwo = require('path/to/two.json');

const combined = [
  jsonOne,
  jsonTwo,
];

But I have almost 40,000 object and I want to do it programmatically one by one. What you are suggesting for that I have to first create the 40,000 JSON file and then require them all and then merge them together which is cumbersome .

That’s important information…

You have a single JSON file, with 40k objects, not in an array? So it’s a single giant object?

It’s hard to help without knowing what your data looks like. Is it in files? Are they coming from a fetch request?

You just have to use JSON.parse() on your data, push inside an array and use JSON.stringify() to convert to JSON again.

https://www.w3schools.com/js/js_json_parse.asp

Try to do It. If you can’t make It work, I’ll write the code for you.

No, it’s not a single giant object. I am getting bellow data in every iteraton.

{"title":"C Squared Media","rating":null,"reviewCount":null,"profileSummary":"At C Squared Media, we help our clients thrive through a focus on the most intelligent solutions, effective communication & a commitment to professionalism every step of the way.  ","minProjSize":"Undisclosed","ratePerHour":"Undisclosed","foundingYear":"Founded 2014","location":"Thiensville, WI","focuses":[{"Advertising":"25%"},{"Marketing Strategy":"25%"},{"Pay Per Click":"25%"},{"Search Engine Optimization":"25%"}],"siteUrl":"https:\u002F\u002Fwww.csquaredmedia.com\u002F"}

I am getting this JSON data on every loop and I want to append it to the JSON file.

For that every time i have to load the entire JSON file and convert them to merge that’s will not be efficient as for a single data I have to write the whole file again.

Well… That’s what functions are for. You’re going to make a function and call It inside your loop.

How are you iterating on the giant object? What are the smaller objects indexed by? An ID number or something?

Is the original data something like this:

{
  123: {
    "title": "some movie",
    "rating": 5
    // ...
  },
  456: {
    "title": "some other movie",
    "rating": 9.9
    // ...
  },
  // ...
}

And you want to change it into:

[
  {
    "title": "some movie",
    "rating": 5
    // ...
  },
  {
    "title": "some other movie",
    "rating": 9.9
    // ...
  },
  // ...
]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.