Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:33:24+00:00 2026-06-11T00:33:24+00:00

I am used to use JSON and Numpy to store arrays, lists and dicts

  • 0

I am used to use JSON and Numpy to store arrays, lists and dicts in python, but I want to use BSON since floating point numbers would occupy only 4 bytes and thus reduce filesize.

With Json, I do the following:

import numpy
import json

a = numpy.random.rand(12).reshape((3,4))

with open('out.json', 'w') as out:
    json.dump(a.tolist(), out)

with open('out.json') as inp:
    b = numpy.array(json.load(inp))

print b

I didn’t find an obvious way to do the same with BSON. I tried this:

import numpy
from bson import BSON

a = numpy.random.rand(12).reshape((3,4))

b = BSON.encode({'a': a.tolist()})

with open('out.bson', 'wb') as out:
    out.write(b)

with open('out.bson', 'rb') as inp:
    print BSON().decode(inp.read())

But get this error:

Traceback (most recent call last):
  File "apaga.py", line 12, in <module>
    print BSON().decode(inp.read())
  File "/usr/lib/python2.7/dist-packages/bson/__init__.py", line 539, in decode
    (document, _) = _bson_to_dict(self, as_class, tz_aware)
bson.errors.InvalidBSON: not enough data for a BSON document
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:33:26+00:00Added an answer on June 11, 2026 at 12:33 am

    The version of BSON I have installed won’t import as shown so maybe I am using a different version. To see your documentation in python type help(bson) after importing…

    Something like this should work:

    import bson
    >>> a = numpy.random.rand(12).reshape((3,4))
    >>> b = bson.dumps({'a':a.tolist()})
    >>> print bson.loads(b)
    {u'a': [[0.033390565943162254, 0.7282666963459123, 0.03719924011978737, 0.2664821209717694], [0.6145164300761253, 0.3662769247564551, 0.5973843055182299, 0.42908933503924207], [0.05901830243140804, 0.31533731904861184, 0.7158207045507905, 0.12686922689849378]]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using json to open the user popup. I used to use basename( $_FILES['userfile']['name']
I'm trying to use AJAX and JSON inside CodeIgniter. I've never used either technologies
Good day! I want to create a function wherein I need to use JSON
I would like to use Python's JSON module. It was only introduced in Python
I used JSON.stringify() to store a javascript associative array of integer keys with boolean
I used to use a guitar tab site and it had a feature where
I used to use procedural-style PHP. Later, I used to create some classes. Later,
I used to use SVN 1.4 on OS X Leopard and everything was fine.
I used to use this script for jquery email obfuscation: $(.replaceAt).replaceWith(@); $(.obfuscate).each(function () {
I used to use Backbone.js as an easy MVC framework which have build-in routing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.