0

I am new to Python and I am trying to parse a Json file using Python. The Json file is a nested file. While I am trying to exact the "conversation_id" item, the list that contains this item, and the list above sometimes can be empty. I am hoping to replace empty list as string "N/A", otherwise grab the item. Code I am using is as following:

for log in data['logs']:
    print("Processing log "+log['log_id'])

    logcolumns=[]

    if log['request'] is None:
            logcolumns.append("N/A")
    elif log['request']['context'] is None:
           logcolumns.append("N/A")
    else:
        logcolumns.append(log['request']['context']['conversation_id'])
try:
        print("\t".join(logcolumns),file = conv_tsv)
    except KeyError:pass

del logcolumns

Traceback error I got is

Processing log cafa1077-f479-4c55-ac34-3bc3ebbb41fc
Traceback (most recent call last):
  File "conversation_log_2.py", line 43, in <module>
    logcolumns.append(log['request']['context']['conversation_id'])
KeyError: 'conversation_id'

The "request" list that is associated with this log id is shown as below in the json file:

{"request": {"input": {}, "context": {}},

A full request list would be like this:

{"request": {"input": {"text": "haha"}, "context": {"conversation_id": "328d2320-f488-4f46-b71f-6cdfb1b79106", "system": {"dialog_stack": [{"dialog_node_s": "root"}], "dialog_turn_counter": 1, "dialog_request_counter": 1, "_node_output_map_s": "{\"Welcome\":[0,1,0]}", "branch_exited_s": "true", "branch_exited_reason_s": "completed"}}}, 

When I went to the output file, which is conv.tsv, there is N/A in the output.

4
  • 2
    Well why would it? {} is not None. Commented Jul 13, 2017 at 14:11
  • Can you please tell me how I can catch the empty strings then? I tried if !log['request'] if log['request']==0 but it always came back as syntax error. Commented Jul 13, 2017 at 14:26
  • 1
    if not log['request']:; Python doesn't use ! for negation like that (see docs.python.org/3/reference/expressions.html#boolean-operations), and '' != 0. Commented Jul 13, 2017 at 14:26
  • Thanks for teaching me how to catch empty object in python too. Commented Jul 13, 2017 at 14:41

1 Answer 1

2

You seem to have the syntax quite moodled up. Is the try/except supposed to be wrapping the if/elif? Do you actually want if/elifs?

Note that log['request'] is None does not test that the key's value is an empty dict.

You can use the .get method that returns a default when the key is not found:

logcolumns.append(log.get('request', {}).get('context', {}).get('conversation', 'N/A'))

Or better still, use a try/except to append the default value if any of the keys in missing:

try:
    logcolumns.append(log['request']['context']['conversation_id'])
except KeyError:
    logcolumns.append('N/A')
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. But I am not trying to append log[ 'request'], neither am I trying to append log['request']['context']. I only want to append log['request']['context']['conversation_id']. Using your approach, I changed my code to: logcolumns.append(log.get('request', 'N/A')) logcolumns.append(log.get('request', {}).get('context', 'N/A')) logcolumns.append(log['request']['context']['conversation_id']) it gave me this error: TypeError: sequence item 0: expected str instance, dict found
The second approach worked great for me. Thanks! Also you were right about my wrong placed try/except. It should have been placed for each item not at the end. I don't actually want 'if/elif' though.
sorry I thought upvoting means accepting. I just accepted. Thanks.

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.