0

i am trying to extract specific data from JSON in python but can't do it without specifying the name aby,adz,agn as the data array is very big and i am posting only part of it.

For example, I would like to get "nethash" of the element whose "order" is 160. What is the best strategy here?

This is my JSON:

{
"data": {
    "aby": {
        "info": {
            "algo": "scrypt",
            "bestPool": 11,
            "bestexchange": 4,
            "blocks": 1036520,
            "blocktime": 0,
            "coin": "aby",
            "coinsPerDay": "0.82148358090787920727",
            "diff": 4383.33555,
            "diffAlgo": "0.00753414531332241653",
            "hashAlgo": "0",
            "nethash": 66289.728291,
            "pos": "?",
            "priceBTC": "0.00000098",
            "priceUSD": "0.00917138879999999934",
            "reward": 200,
            "timestamp": "2018-05-10 08:51:02.782957",
            "type": "diff",
            "usdPerDay": "0.00753414531332241653",
            "value": "0.00753414531332241653",
            "workers": 757
        },
        "order": 109
    },
    "adz": {
        "info": {
            "algo": "x11",
            "bestPool": 10,
            "bestexchange": "None",
            "blocks": 422294,
            "blocktime": 0,
            "coin": "adz",
            "coinsPerDay": "0.15507838730965944896",
            "diff": 103774.174,
            "diffAlgo": "0.00310020305638486395",
            "hashAlgo": "0",
            "nethash": 612234.455356,
            "pos": "?",
            "priceBTC": "0.00000214",
            "priceUSD": "0.01999120000000000064",
            "reward": 40,
            "timestamp": "2018-05-10 08:51:02.782957",
            "type": "diff",
            "usdPerDay": "0.00310020305638486395",
            "value": "0.00310020305638486395",
            "workers": 265
        },
        "order": 160
    },
    "agn": {
        "info": {
            "algo": "neoscrypt",
            "bestPool": 10,
            "bestexchange": 5,
            "blocks": 58301,
            "blocktime": 0,
            "coin": "agn",
            "coinsPerDay": "51.22860596982359027152",
            "diff": 4.47654431,
            "diffAlgo": "1.47183776684280331892",
            "hashAlgo": "0",
            "nethash": 86.217988,
            "pos": "?",
            "priceBTC": "0.00000307",
            "priceUSD": "0.02873077919999999716",
            "reward": 6,
            "timestamp": "2018-05-10 08:51:02.782957",
            "type": "diff",
            "usdPerDay": "1.47183776684280331892",
            "value": "1.47183776684280331892",
            "workers": 417
        },
        "order": 61
    }
},
"message": "",
"status": "ok",
"timestamp": "Thu, 10 May 2018 08:51:07 GMT"

}

2
  • 1
    possible duplicate of: stackoverflow.com/questions/31222195/… Commented May 10, 2018 at 18:34
  • What have you tried already? Have you started with setting the json value to a variable and then processing that variable? This should only take 2-3 lines Commented May 10, 2018 at 18:40

2 Answers 2

0

You could iterate over the keys of the JSON until you find what you are searching for:

data = json_data['data']
for i in data.keys():
    if data[i]['order'] == 160:
        nethash = data[i]['info']['nethash']

Edit: Here is some possible code for searching for multiple values, I tested and it works but not sure how efficient it would be for a large dataset:

data = json_data['data']
search_list = [160, 61]
nethash_dict = {}
for i in data.keys():
    if data[i]['order'] in search_list:
        nethash_dict[data[i]['order']] = data[i]['info']['nethash']
Sign up to request clarification or add additional context in comments.

2 Comments

One more thing, do i need to run this FOR loop everytime for getting every order? lets say i want 'order' == 61 and 'order' == 160
I updated my answer with some possible code for searching and pulling out multiple values into a dictionary.
0

The best strategy is to define by you:

You can use libraries like objectpath, it makes you able to search in JSON in easy way. just import the library and build the object tree, then type your word that you want to search for.

also you can build your own class or function for search in json, some like:

obj = CustomJsonSearch(..{your data})
query = obj.return_value(search_id=168, retunr_params=['nethash'])

or use a DATABASE like postgres and make queries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.