How to upload multiple documents with multiple JSON files to Cloudant DB via cURL? How to upload multiple documents with multiple JSON files to Cloudant DB via cURL? json json

How to upload multiple documents with multiple JSON files to Cloudant DB via cURL?


So you definitely want to use Cloudant's _bulk_docs API endpoint in this scenario. It's more efficient (and cost-effective) if you're doing a bunch of writes. You basically POST an array that contains all your JSON docs. Here's the documentation on it: https://docs.cloudant.com/document.html#bulk-operations

Going one step further, so long as you've structured your JSON file properly, you can just upload the file to _bulk_docs. In cURL, that would look something like this: curl -X POST -d @file.json <domain>/db/_bulk_docs ... (plus the content type and all that other verbose stuff).

One step up from that would be using the ccurl (CouchDB/Cloudant cURL) tool that wraps your cURL statements to Cloudant and makes them less verbose. See https://developer.ibm.com/clouddataservices/2015/10/19/command-line-tools-for-cloudant-and-couchdb/ from https://stackoverflow.com/users/4264864/glynn-bird for more.

Happy Couching!


You can create a for loop and create documents from each JSON file.

For example, in the command below I have 4 JSON files in my directory and I create 4 documents in my people database:

for file in *.json> do> curl -d @$file https://username:password@myinstance.cloudant.com/people/ -H "Content-Type:application/json"> done{"ok":true,"id":"763a28122dad6c96572e585d56c28ebd","rev":"1-08814eea6977b2e5f2afb9960d50862d"}{"ok":true,"id":"763a28122dad6c96572e585d56c292da","rev":"1-5965ef49d3a7650c5d0013981c90c129"}{"ok":true,"id":"763a28122dad6c96572e585d56c2b49c","rev":"1-fcb732999a4d99ab9dc5462593068bed"}{"ok":true,"id":"e944282beaedf14418fb111b0ac1f537","rev":"1-b20bcc6cddcc8007ef1cfb8867c2de81"}