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

in python •  7 years ago  (edited)

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!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  7 years ago (edited)

I think you should be able to achieve the same withjson.loads()
(untested code)

import json
[...]
for h in a.get_account_history(-1,10000, filter_by=["custom_json"]):
  jsdata = json.loads(h['json'])
  if jsdata[0] == "reblog":
    [...]

The raw json could potentially contain spaces between strings and brackets etc, which your approach might miss.

I didn't take that into account.

It seems to work for my purposes at the moment but I realize I wouldn't work for anything other than reblogs the way it is written (I may be able to split on the first comma but still not sure if there would be a situation such as you described that would screw it all up).

The method looks like to could be used for for a universal conversion so I'll get it tested and let you know!

Thanks, @crokkon!

Beep! Beep! @shadow3scalpel at your service. I am here to assist all military members on Steemit. This HumVee will be scouting posts from a list of Veterans that is maintained by @chairborne. If you are a Veteran and new to Steemit, and you have questions or want to join the Veterans community, reply to this comment. We got your six, unless you are in the rear with the gear. Ooh-Rah!
Comment by @inthenow. This is a opt-in bot.

YOU JUST GOT UPVOTED

Congratulations,
you just received a 47.94% upvote from @steemhq - Community Bot!

Wanna join and receive free upvotes yourself?
Vote for steemhq.witness on Steemit or directly on SteemConnect and join the Community Witness.

This service was brought to you by SteemHQ.com