0

i have the following json file, the "ticket" python variable contains the "data" part:

{
"date": "139096543" 
"data": [
    {
    "author": {
                "title": "User",
                "champion": false,
                "canonical_name": "tahlia_moffitt",
                "id": 6944932
    },
    "is_spam": false,
    "me_too_count": 2,
    "status": "complete"
    }
]
}

I used the following python script to get the data for example from "status":

'status': ticket.get('status') or 'NoStatus'

But i need the data from the author subclass. I tried as:

'title': ticket.get('author.title') or 'NoTitle'

and

'title': ticket.get('author[title]') or 'NoTitle'

too, but they didnt work for me. Can anybody propose a solution for me?

3
  • That's not really an appropriate use of "subclass", which implies inheritance. Commented Oct 15, 2014 at 10:53
  • I'm still looking for the closing ]. I hope this is not your real json file, as it seems improperly formatted. Commented Oct 15, 2014 at 10:54
  • @Flav yes i only tried to cut a sample from to json which explain my question Commented Oct 15, 2014 at 11:27

1 Answer 1

1

Try this construction:

'title': ticket.get('author',{}).get(title', 'NoTitle')

Construction

get(title', 'NoTitle')

Will return value if present, if not will return second value.

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

2 Comments

Thank you it worked! Is the " get(title', 'NoTitle') " construction better then the " get('title') or 'NoTitle' " ?
Not exactly shorter, but tells you what you want to do:) It's a getter from a dict, with a default value.

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.