Python to Convert String Representation of JSON (On Reblogs) to Dictionary

This might just help you if you are dealing with custom_json account history IE resteems


Let's say you have obtained custom_json from account history using the get_account_history method of a Steem Account (using Steem Python for example) but are looking for a way to reassemble and work with the data without having comprehensive knowledge of the necessary regex (regular expression) needed to work with the data.

In my case, I am trying to put back together a post identifier so need to use the author and permlink values. I set up a function to assist me with this in the future after a bit of tinkering and research.

import ast
def reassemble_dict(jsonstr):
    split = jsonstr.split('["reblog",')[1]
    split = split(']')[0]
    json_dict = ast.literal_eval(split)
    return json_dict

As an added bonus, I will be sharing my get_reblog function to help you understand the context:

def get_reblogs(user,steem)
    a = Account(user,steem)
    cjson = []
    reblog = []
    hist = a.get_account_history(-1,10000, filter_by=["custom_json"])
    for h in hist:
        cjson.append(j)
    for j in cjson:
        if 'reblog' in j['json']:
            reblog.append(j)
    return reblog

The above will return a list of dictionaries but it just so happens that the json attribute will be of type string which I find mighty inconvenient. Anyways, this function will get the job done when you pass that string as a parameter. Hope it helps.

You may be wondering why I pass my steem instance to my functions and, to be honest, I don't rightly know spare it seemed to have resolved some issues I has having a while ago. It may not be needed but I am one to operate under the philosophy "If it isn't broke, don't fix it" so have opted to leave it the hell alone.

Anyways, that's my tidbit for the day. If you have any questions, feel free to drop a comment. Later!

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now