to json ?
Closed this issue · 5 comments
list_invoices()
returns a rpc_pb2.ListInvoiceResponse
.
invoices {
r_preimage: "\030\324\300\2421\232\341p\306\237\2662\210\335\035\326\256\346W\226\025\367\302\300\362\\\301\221\237\031\267\360"
r_hash: "\362\303\271\025RG\014x[!\206\346(k+\0226c\301\256u\305\247\204@\320e\235\213Q\334\325"
value: 100
creation_date: 1528903408
payment_request: "lntb1u1pdjzdhspp57tpmj92jgux8skepsmnzs6etzgmx8sdwwhz60pzq6pjemz63mn2sdqqcqzys2xdd44knpwswu97txpucvxeuhqh0vcrlxk7ufzs4563lm8eeqgehlyma9aszeyfzyp97khtcuds2x4evx8g7ga9fczkn3e9qz9frcgsplgucyw"
expiry: 3600
cltv_expiry: 144
}
invoices {
r_preimage: "\347\357\343F7nB\024\212\365\347\333a5\242\214{\031+\237\240\026\254\213\006\262\231\314\202\031X\177"
r_hash: "~\353\273\265\372\025\252e\303\356\357\225\314\361t\2770\301\321\262!\303\217\200\310\375\320obF1x"
value: 100
creation_date: 1528903465
payment_request: "lntb1u1pdjzdefpp50m4mhd06zk4xtslwa72ueut5hucvr5djy8pclqxglhgx7cjxx9uqdqqcqzys0chnhs4cxdvd6tmawft5f5mncm57glha2rh477fgmfvju6a3khs8w77yg5073h3nhmtxgt7wkw08c7f5l564hx8wfgsupyvy5u49wfqqks3gvy"
expiry: 3600
cltv_expiry: 144
}
Is there an easy way to get this as a json or dict?
nevermind..
from google.protobuf.json_format import MessageToJson
seems to work just fine
@FeedtheWeb glad you figured it out - I've been debating weather to convert everything to list/dict (native python types) vs keeping the responses in the form of the grpc classes.
I too have been converting to dict a lot so maybe I'll add an option where one can just set it and it will run MessageToJson
on all responses (where appropriate).
I think that's a good idea. The tip above helped me as well (I was trying to get network graph). But it would be easier built in.
Agree that this would be a very useful method to have. I had a look at the code and wasn't sure where to add it. Instead, perhaps it is easier to just do from google.protobuf.json_format import MessageToJson
in the main module so people can use that function easily? Or mention in the README.
Cheers for the help
import json
from google.protobuf.json_format import MessageToJson
channels = lnd.list_channels()
json.loads(MessageToJson(channels))
to get it all the way to a dict