Jump to content
ameliabob

converting string to Dict

Recommended Posts

I am receiving a string and I can't see to get it into a dictionary..

For example I have a string:

[{"rowId": 21,"nature": "O","symbol": "TAL","qty": 10,"daysHeld": 0,"source": "Momentum","dayEntered": "2018-01-16","pl": -40},

{"rowId": 22,"nature": "O","symbol": "SGMO","qty": 5,"daysHeld": 35,"source": "Lightning Trend","dayEntered": "2018-02-06","pl": 2300},

{"rowId": 23,"nature": "O","symbol": "NVDA","qty": 10,"daysHeld": 61,"source": "Tactical Trader","dayEntered": "2018-02-09","pl": -15300},

{"rowId": 24,"nature": "O","symbol": "ALGN","qty": 10,"daysHeld": 88,"source": "none","dayEntered": "2018-01-18","pl": -17100},

{"rowId": 25,"nature": "O","symbol": "IMGN","qty": 10,"daysHeld": 67,"source": "Lightning Trend","dayEntered": "2018-02-08","pl": 460}]

How do I make this into a dict?

How would I retreive the 'dayentered ' data from the line with symbol ALGN?

 

Thanx

Share this post


Link to post
Share on other sites

Each entry needs a unique key, so decide what you want to use for the key for each entry.  You can make each of those JSON objects a dictionary itself, and put all of them in another dictionary.

Share this post


Link to post
Share on other sites
needle='ALGN'

whatever =  [{"rowId": 21,"nature": "O","symbol": "TAL","qty": 10,"daysHeld": 0,"source": "Momentum","dayEntered": "2018-01-16","pl": -40},

{"rowId": 22,"nature": "O","symbol": "SGMO","qty": 5,"daysHeld": 35,"source": "Lightning Trend","dayEntered": "2018-02-06","pl": 2300},

{"rowId": 23,"nature": "O","symbol": "NVDA","qty": 10,"daysHeld": 61,"source": "Tactical Trader","dayEntered": "2018-02-09","pl": -15300},

{"rowId": 24,"nature": "O","symbol": "ALGN","qty": 10,"daysHeld": 88,"source": "none","dayEntered": "2018-01-18","pl": -17100},

{"rowId": 25,"nature": "O","symbol": "IMGN","qty": 10,"daysHeld": 67,"source": "Lightning Trend","dayEntered": "2018-02-08","pl": 460}]

for row in whatever:
    if needle == row['symbol']:
        print ('Specific Item')
        print(row['dayEntered'])
        print ("All items from specific row")
        for key, value in row.items():
            print ("key: ",key,", value: ", value)

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...