Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert BSON to JSON with human-readable date format

I would like to transform a BSON dump of MongoDB to JSON.

To do that, I'm using the bsondump tool provided with Mongo, but I get an output like :

{ "_id" : ObjectId( "5316d194b34f6a0c8776e187" ), "begin_date" : Date( 1394004372038 ), "foo" : "bar" } { "_id" : ObjectId( "5316d198b34f6a0c8776e188" ), "begin_date" : Date( 1394004407696 ), "foo" : "bar" } 

Can anyone tell me how to get the dates appear in a human readable format (e.g. hh:mm:ss dd/mm/yyyy) ?

Edit

It looks like that a more recent version of mongodump outputs dates as:

{ "_id" : ObjectId( "5316d194b34f6a0c8776e187" ), "begin_date" : {"$date":"2015-11-11T08:45:03.974Z"}}, "foo" : "bar" } 

So this question is not relevant anymore. Thanks everybody for your help here.

like image 263
vcarel Avatar asked Apr 11 '14 10:04

vcarel


People also ask

Can I convert BSON to JSON?

Synopsis. The bsondump converts BSON files into human-readable formats, including JSON.

Is BSON human-readable?

BSON needs to be parsed as they are machine-generated and not human-readable.

How do I read a BSON file?

If you cannot open your BSON file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a BSON file directly in the browser: Just drag the file onto this browser window and drop it.


1 Answers

bsondump converts BSON files into human-readable formats, including JSON. For example, bsondump is useful for reading the output files generated by mongodump.

Source: https://docs.mongodb.com/manual/reference/program/bsondump

Examples

bsondump --outFile collection.json collection.bson 

The --pretty option outputs documents in a pretty-printed format JSON, eg:

bsondump --pretty --outFile collection.json collection.bson 
like image 79
Sanjeev Kumar Rai Avatar answered Sep 22 '22 06:09

Sanjeev Kumar Rai