Jump to content

converting string to Dict


ameliabob

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

Link to comment
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.

Link to comment
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)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...