2

I'm on an Ubuntu system, and I'm trying to write a testing framework that has to (among other things) compare the output of a mongodump command. This command generates a bunch of BSON files, which I can compare. However, for human readability, I'd like to convert these to nicely formatted JSON instead, which I can do using the provided bsondump command. The issue is that this appears to be a one-way conversion.

While I can work around this if I absolutely need to, it would be alot easier if there was a way to convert back from JSON to BSON on the command line. Does anyone know of a command line tool to do this? Google seems to have come up dry.

7
  • I don't follow. What's the goal with the BSON file that you would create? Commented Jun 27, 2013 at 19:11
  • 1
    To then load back into the Mongo DB. Essentially I want the following connectivity: Mongo <-> BSON <-> JSON I have 3 out of the 4 links required to make this work. Commented Jun 27, 2013 at 19:14
  • Why not just import it directly as JSON via mongoimport? Commented Jun 27, 2013 at 19:22
  • Honestly, didn't realize there were two sets of import/export tools here. I'm still incredibly new to Mongo. From the mongoimport docs, I read "... Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore ..." -- Not sure exactly what information is and isn't captured, but I think I'm going to play it safe and stick with importing the BSON dumps. That way I know the test tests every single record in the DB. Commented Jun 28, 2013 at 13:45
  • 1
    I don't follow your comment, nor why you're trying to import data via the command line tools for testing. (Why are you trying to reimport data that was exported)? A JSON file cannot necessarily represent all of the data types of BSON adaquately for the purposes of an import. Commented Jun 28, 2013 at 14:48

3 Answers 3

1

I haven't used them, but bsontools can convert from json, xml, or csv

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

Comments

0

As @WiredPrarie points out, the conversion from BSON to JSON is lossy, and it makes no sense to want to go back the other way. Workarounds include using mongoimport instead of mongorestore, or just using the original BSON. See the comments for more deails (adding this answer mainly so I can close the question)

Comments

0

You can try beesn, it converts data both ways. For your variant - JSON -> BSON - use the -x switch.

Example:

$ beesn -x -i test-data/01.json -o my.bson

Disclaimer: I am an author of this tool.

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.