skip to Main Content

I have used an online helper, https://jsonpath.com/ to find a working jsonpath to extract a value from the attacehd json structure. The expressionis as follows:

$.activations[0].bidReference

How can I translate this into "python"? I have tried this:

jdata = response.json()
bidreference = jdata["activations[0].bidReference"]

But I get a "KeyError".

Here is the json response:

    {
  "orderReference": "88a13d98-f9c9-49cb-8682-2ec2ef27bc73",
  "requestReference": "106d1b23-978a-4894-a1e1-e3de2b0c0f02",
  "createdUtc": "2024-08-06T12:22:30+00:00",
  "receivedUtc": "0001-01-01T00:00:00+00:00",
  "startUtc": "2024-08-06T12:30:00+00:00",
  "endUtc": "2024-08-06T12:45:00+00:00",
  "numberOfActivations": 1,
  "activations": [
    {
      "requestReference": "106d1b23-978a-4894-a1e1-e3de2b0c0f02",
      "orderReference": "88a13d98-f9c9-49cb-8682-2ec2ef27bc73",
      "bidReference": "e810ff33-4946-425d-9c63-1584125efc9b",
      "status": "Activated",
      "direction": "UP",
      "startUtc": "2024-08-06T12:30:00+00:00",
      "endUtc": "2024-08-06T12:45:00+00:00",
      "quantity": 15,
      "reasonCode": null,
      "reasonText": null,
      "powerPlantName": "Nore 1",
      "powerPlantId": 7303,
      "priceAreaId": 1,
      "regionId": 10
    }
  ],
  "events": [
    {
      "activationReference": "88a13d98-f9c9-49cb-8682-2ec2ef27bc73",
      "activationRequestReference": "106d1b23-978a-4894-a1e1-e3de2b0c0f02",
      "eventType": "AcceptedAcknowledgeOut",
      "eventMessage": "Activation request from TSO was acknowledged",
      "eventUtc": "2024-08-06T12:22:32.604432+00:00"
    },
    {
      "activationReference": "88a13d98-f9c9-49cb-8682-2ec2ef27bc73",
      "activationRequestReference": "106d1b23-978a-4894-a1e1-e3de2b0c0f02",
      "eventType": "AcceptedOut",
      "eventMessage": "1 accepted and 0 rejected activations sent to TSO",
      "eventUtc": "2024-08-06T12:22:32.832895+00:00"
    },
    {
      "activationReference": "88a13d98-f9c9-49cb-8682-2ec2ef27bc73",
      "activationRequestReference": "106d1b23-978a-4894-a1e1-e3de2b0c0f02",
      "eventType": "AcceptedAcknowledgeIn",
      "eventMessage": "TSO accepted acknowledge",
      "eventUtc": "2024-08-06T12:22:34.387739+00:00"
    }
  ],
  "activationType": "ACTIVATION",
  "activationRelations": [
    {
      "reference": "e810ff33-4946-425d-9c63-1584125efc9b",
      "requestReference": "106d1b23-978a-4894-a1e1-e3de2b0c0f02",
      "powerPlantId": 7303,
      "powerPlantName": "Nore 1",
      "priceAreaId": 1,
      "regionId": 10
    }
  ],
  "error": false
}

2

Answers


  1. Keys and indexes in python have a different syntax. Each key or index address should be done within separate [ ] pair. You can learn more on accessing nested dicts (although you also have nested lists mixed in too) here, for example.

    I believe this should work for your case:

    jdata = response.json()
    bidreference = jdata["activations"][0]["bidReference"]
    
    Login or Signup to reply.
  2. you are trying to access the nested elements in the wrong way. try to access it like this way:

    jdata = response.json()
    bid_reference = jdata["activations"][0]["bidReference"]
    

    where:

    • jdata["activations"] gets the list of activations.
    • [0] accesses the first (and in your case, only) item in the list.
    • ["bidReference"] accesses the value of the bidReference key in the dictionary.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search