0

I have the following json:

    [{'errors': [],
  'configErrors': [],
  'summary': {'pullrequest': {'overall': {'count': 0,
     'lastUpdated': None,
     'stateCount': 0,
     'state': 'OPEN',
     'open': True},
    'byInstanceType': {}},
   'build': {'overall': {'count': 0,
     'lastUpdated': None,
     'failedBuildCount': 0,
     'successfulBuildCount': 0,
     'unknownBuildCount': 0},
    'byInstanceType': {}},
   'review': {'overall': {'count': 0,
     'lastUpdated': None,
     'stateCount': 0,
     'state': None,
     'dueDate': None,
     'overDue': False,
     'completed': False},
    'byInstanceType': {}},
   'deployment-environment': {'overall': {'count': 0,
     'lastUpdated': None,
     'topEnvironments': [],
     'showProjects': False,
     'successfulCount': 0},
    'byInstanceType': {}},
   'repository': {'overall': {'count': 0, 'lastUpdated': None},
    'byInstanceType': {}},
   'branch': {'overall': {'count': 0, 'lastUpdated': None},
    'byInstanceType': {}}}}

I need to retrieve value count. I've tried with a loop for:

for item in json['summary']['pullreqest']['overall']:
     value = item['count']

but I have an error must be integers or slices, not str .

1 Answer 1

3

The json looks like an array, I fixed a typo (pulrequest) and simplified the retrieval of the item.

item = json[0]['summary']['pullrequest']['overall']
print(item)
value = item['count']
print(value)

When you loop through json[0]['summary']... what you are doing is taking the keys, but not the tuples, so you could not do item['count'].

All in all you are working with a Python dictionary and are bound by its rules.

Sign up to request clarification or add additional context in comments.

4 Comments

@inspiredd Is that the whole JSON? Now that I look at it it's not valid.
I've added the whole JSON.
@inspiredd fixed my answer, is a bit different but I hope you find it useful. Also your new JSON is still missing the "]" at the end, just in case!
thanks a lot. I had 'key error 0' but I deleted 0 and it works perfect.

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.