Search code examples
pythonjsonrpcjson-rpc

Python json-rpc help, how to extract data


So I have been playing with bitcoin-qt and python. Bitcoin uses json-rpc. I managed to extract the raw data from bitcoin-qt using python but have absolutely no idea how I can extract individual parts of the data and store it in a variable.

My python code is as follow:

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import json

access = AuthServiceProxy("http://user:[email protected]:8332")
print access.getinfo()

And the raw data output looks like this: {u'connections': 15, u'errors': u'', u'blocks': 352896, u'paytxfee': Decimal('0E-8'), u'keypoololdest': 1407840711, u'walletversion': 60000, u'difficulty': Decimal('47610564513.47126007'), u'testnet': False, u'version': 100000, u'proxy': u'127.0.0.1:9050', u'protocolversion': 70002, u'timeoffset': -1, u'balance': Decimal('0.00099760'), u'relayfee': Decimal('0.00001000'), u'keypoolsize': 101}

Lets say I want to extract the balance, from this data, how do I do that?


Solution

  • Access by the key, you may also need to import Decimal:

    from decimal import Decimal
    
    print(access.getinfo()["balance"])
    

    So just assign to the value returned:

     bal, pay_tax = access.getinfo()["balance"], access.getinfo()["paytxfee"]
     .....