13

If I have a json file that looks something like this:

{"name":"bob","hi":"hello"}
{"name":"hello","hi":"bye"}

Is there an option to import this into couchdb?

7 Answers7

7

Starting from @Millhouse answer but with multiple docs in my file I used

cat myFile.json | lwp-request -m POST -sS "http://localhost/dbname/_bulk_docs" -c "application/json" 

POST is an alias of lwp-request but POST doesn't seem to work on debian. If you use lwp-request you need to set the method with -m as above.

The trailing _bulk_docs allows multiple documents to be uploaded at once.

http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API

Subleak
  • 135
  • 5
thelastshadow
  • 3,406
  • 3
  • 33
  • 36
5

If you are on Linux, You could write a quick shell script to POST the contents of valid json files to Couch.

To test couch I did something like this:

cat myFile.json | POST -sS "http://myDB.couchone.com/testDB" -c "application/json"

myFile.json has the json contents I wanted to import into the database.

Another alternative, if you don't like command line or aren't using Linux, and prefer a gui, you can use a tool like RESTClient

Millhouse
  • 732
  • 1
  • 8
  • 17
4

Probably a bit late to answer. But If you can use Python than you can use the couchdb module to do so:

import couchdb
import json
couch = couchdb.Server(<your server url>)
db = couch[<your db name>]
with open(<your file name>) as jsonfile:
    for row in jsonfile:
        db_entry = json.load(row)
        db.save(db_entry)

I created the python script to do that(As I could not find one on Internet).

The full script is here: :

http://bitbucket.org/tdatta/tools/src/

(name --> jsonDb_to_Couch.py)

If you download the full repo and:

  1. Text replace all the "_id" in json files to "id"

  2. Run make load_dbs

It would create 4 databases in your local couch installation

Hope that helps newbies (like me)

Tanmay
  • 98
  • 6
  • 1
    I believe that your code is supposed to have the following changes: `couch = couchdb.server()` missing url `with open()` close parenthesis instead of bracket `db_entry = json.loads(row)` loads instead of load – Jared Beach Apr 25 '16 at 05:13
  • Thanks Jared. Fixed it in the original post. – Tanmay Apr 27 '16 at 08:37
3

Yes, this is not valid JSON ...

To import JSON-Objects I use curl (http://curl.haxx.se):

curl -X PUT -d @my.json http://admin:secret@127.0.0.1:5984/db_name/doc_id

where my.json is a file the JSON-Object is in. Of course you can put your JSON-Object directly into couchdb (without a file) as well:

curl -X PUT -d '{"name":"bob","hi":"hello"}' http://admin:secret@127.0.0.1:5984/db_name/doc_id

If you do not have a doc_id, you can ask couchdb for it:

curl -X GET http://127.0.0.1:5984/_uuids?count=1
user470370
  • 572
  • 1
  • 5
  • 17
  • For anyone else getting an error about Content-Type on the first example here, try this: `curl -X POST -d @data.json http://127.0.0.1:5984/database/_bulk_docs -H 'Content-Type: application/json'` – Eric Uldall Oct 22 '14 at 23:33
  • Downvoted because I think the question is asking about multiple docs and not just one. I believe you are answering how to import only one doc. – Jared Beach Apr 25 '16 at 05:04
2

It's not my solution but I found this to solve my issue:

A simple way of exporting a CouchDB database to a file, is by running the following Curl command in the terminal window:

curl -X GET http://127.0.0.1:5984/[mydatabase]/_all_docs\?include_docs\=true > /Users/[username]/Desktop/db.json

Next step is to modify the exported json file to look like something like the below (note the _id):

{
  "docs": [
      {"_id": "0", "integer": 0, "string": "0"},
      {"_id": "1", "integer": 1, "string": "1"},
      {"_id": "2", "integer": 2, "string": "2"}
  ]
}

Main bit you need to look at is adding the documents in the “docs” code block. Once this is done you can run the following Curl command to import the data to a CouchDB database:

curl -d @db.json -H "Content-type: application/json" -X POST http://127.0.0.1:5984/[mydatabase]/_bulk_docs

Duplicating a database If you want to duplicate a database from one server to another. Run the following command:

curl -H 'Content-Type: application/json' -X POST http://localhost:5984/_replicate -d ' {"source": "http://example.com:5984/dbname/", "target": "http://localhost@:5984/dbname/"}'

Original Post: http://www.greenacorn-websolutions.com/couchdb/export-import-a-database-with-couchdb.php

2

That JSON object will not be accepted by CouchDB. To store all the data with a single server request use:

{
  "people": 
   [
      {
        "name":"bob",
        "hi":"hello"
      },
      { 
        "name":"hello",
        "hi":"bye"
      }
   ]
}

Alternatively, submit a different CouchDB request for each row.

Import the file into CouchDB from the command-line using cURL:

curl -vX POST https://user:pass@127.0.0.1:1234/database \
  -d @- -# -o output -H "Content-Type: application/json" < file.json
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
0

http://github.com/zaphar/db-couchdb-schema/tree/master

My DB::CouchDB::Schema module has a script to help with loading a series of documents into a CouchDB Database. The couch_schema_tool.pl script accepts a file as an argument and loads all the documents in that file into the database. Just put each document into an array like so:

[ {"name":"bob","hi":"hello"}, {"name":"hello","hi":"bye"} ]

It will load them into the database for you. Small caveat though I haven't tested my latest code against CouchDB's latest so if you use it and it breaks then let me know. I probably have to change something to fit the new API changes.

Jeremy

Jeremy Wall
  • 23,907
  • 5
  • 55
  • 73