0

I have an example like this:

listing = {
    "ret_code": 0,
    "ret_msg": "ok",
    "ext_code": "",
    "result": {
        "pages": 10,
        "data": [                <--- would like to get ALL information under "data"
            {
                "user_id": 1,
                "qty": 2,
                "order_status": "Filled",
                "ext_fields": {
                    "close_on_trigger": true,
                    "orig_order_type": "BLimit",
                    "o_req_num": -34799032763,
                    "xreq_type": "x_create"
                },
                "last_exec_price": 7070.5,
                "leaves_qty": 0,
             ]
[... snip ...]
}

Please note: the ~full text~ being worked with is on the "Response" structure on this page: https://bybit-exchange.github.io/docs/inverse/#t-getactive

What I would like to do is get the information under the "data" attribute (i.e. something like listing["data"]). I would like to get back everything under the "data" attribute.

How can this be done?

ETA: I have tried:

 data =  [item for item in listing if item.attribute == 'data']

But my result was as follows:

Traceback (most recent call last):
  File "../trade_bybit/trade_bybit.py", line 198, in get_recent_orders
    data =  [item for item in listing if item.attribute == 'data']
  File "../trade_bybit/trade_bybit.py", line 198, in <listcomp>
    data =  [item for item in listing if item.attribute == 'data']
AttributeError: 'dict' object has no attribute 'attribute'

ETA: @Joshua Varghese Thanks for the hint. I tried

[item for item in listing if item == 'data']

but the answer I got was:

[]
1
  • Why use item.attribute when you can do [item for item in listing if item == 'data'] Commented May 21, 2020 at 2:26

1 Answer 1

1

It looks like "data" is a key in the dictionary corresponding to the key "results".
So therefore, use:

[listing["results"][item] for item in listing["results"] if item == 'data']

or just:

[value for key,value in listing["results"].items() if key == 'data']

If the entire structure contains only one "data", yes, you can use:

data = listing[0]['result']['data']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the hint :) What really did it for me was: data = listing[0]['result']['data'] - it exactly got the data needed. I snipped off some pieces (to make the post smaller) but I will put in a full example of what was being "worked on"

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.