Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post Multiple JSON Objects Simultaneously with Express and Postman

I've been researching this question to no-ends, but can't find the simple answer I'm looking for. Basically, I'd like to batch POST JSON objects in array.

I've got a a giant array of JSON objects.

[ 
 {
   "Name": "SEARCH Resource Center",
    "Address": "2505 Fannin St, Houston, TX 77002",
    "Phone": "(713) 739-7752",
    "Hours": "Mon-Fri, 8am to 3pm",
    "Category": "Drop-In Centers"
 },
 {
   "Name": "Salvation Army Social Services - Young Adult Resource Center",
   "Address": "2208 Main St, Houston, TX 77002",
   "Phone": "(713) 658-9205",
   "Hours": "Mon-Thurs, 11am to 3pm",
   "Category": "Drop-In Centers"
 },
 ...
]

I'm using an Express server that handles post requests looks like this:

app.post('/api/orgs', function(req, res) {

  // Creates a new User based on the Mongoose schema and the post body
  var newOrg = new Organization(req.body);

  // New User is saved in the db.
  newOrg.save(function(err){
    if(err)
      res.send(err);

    // If no errors are found, it responds with a JSON of the new user
    res.json(req.body);
  });
});

These objects are then saved in MongoDB as individual records.

I'm using POSTMAN to send HTTP POSTs to my Express Server. As of now, I've been sending all of my JSON POSTS one at a time, because I can't figure out the best way to batch post all the sub-objects stored in the array as individual objects.

Any suggestions or best practices?

like image 257
Ahmed Haque Avatar asked Dec 05 '25 10:12

Ahmed Haque


2 Answers

If you send you array as a key in your request body, something like this

enter image description here

You'll get it as req.body.my_restaurants. Then simply use this :

db.collection('restaurants').insertMany(req.body.my_restaurants, function(err, restaurants){
    if(err) console.log(err);
    else console.log("restaurants Added Successfully");
});

I'm assuming that restaurants is the name of your collection.

like image 187
SiddAjmera Avatar answered Dec 08 '25 00:12

SiddAjmera


I've been playing with the previous solution I marked as correct. An even better approach than using the .insertMany() function is to use the .create() function.

The .insertMany() function skips the middleware associated with .save(), whereas the create() function uses the same process, but can also handle arrays.

So my modified express route looked like the below (where Organization is the name of my schema):

app.post('/api/orgs', function(req, res) {

// Array of JSON Objects
if (req.body.batch){
  Organization.create(req.body.batch, function(err){
    if(err)
      res.send(err);

    else
      res.json(req.body);
  });
}
// Single JSON Object
else {
  var newOrg = new Organization(req.body);

  // New User is saved in the db.
  newOrg.save(function(err){
    if(err)
      res.send(err);

    // If no errors are found, it responds with a JSON of the new user
    else
      res.json(req.body);
  });
}
});

And I'm sending JSON objects that look like:

JSON in POSTMAN

Hope this helps someone else down the line.

like image 27
Ahmed Haque Avatar answered Dec 07 '25 23:12

Ahmed Haque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!