access json file error – string indices must be integers

Question:

I have the following problem, and I am not sure how to access the item from a nested json file.
Could anyone help me out here, please!

intents = {"intents": [
    {"tag": "greeting",
     "patterns": ["Hi", "Hey", "Is anyone there?", "Hello", "Hay"],
     "responses": ["Hello", "Hi", "Hi there"]
    },
    {"tag": "goodbye",
     "patterns": ["Bye", "See you later", "Goodbye"],
     "responses": ["See you later", "Have a nice day", "Bye! Come back again"]
    },
    {"tag": "thanks",
     "patterns": ["Thanks", "Thank you", "That's helpful", "Thanks for the help"],
     "responses": ["Happy to help!", "Any time!", "My pleasure", "You're most welcome!"]
    },
    {"tag": "about",
     "patterns": ["Who are you?", "What are you?", "Who you are?" ],
     "responses": ["I.m Joana, your bot assistant", "I'm Joana, an Artificial Intelligent bot"]
    },
    {"tag": "name",
    "patterns": ["what is your name", "what should I call you", "whats your name?"],
    "responses": ["You can call me Joana.", "I'm Joana!", "Just call me as Joana"]
    },
    {"tag": "help",
    "patterns": ["Could you help me?", "give me a hand please", "Can you help?", "What can you do for me?", "I need a support", "I need a help", "support me please"],
    "responses": ["Tell me how can assist you", "Tell me your problem to assist you", "Yes Sure, How can I support you"]
    },
    {"tag": "createaccount",
    "patterns": ["I need to create a new account", "how to open a new account", "I want to create an account", "can you create an account for me", "how to open a new account"],
    "responses": ["You can just easily create a new account from our web site", "Just go to our web site and follow the guidelines to create a new account"]
    },
    {"tag": "complaint",
    "patterns": ["have a complaint", "I want to raise a complaint", "there is a complaint about a service"],
    "responses": ["Please provide us your complaint in order to assist you", "Please mention your complaint, we will reach you and sorry for any inconvenience caused"]
    }
]
}

I wanted to print out [‘intents’], see my attemps below.
Here is my attempt, but I got an error as shown below:

new_intents = json.dumps(intents, indent=4)
with open('json_data.json', 'w') as outfile:
    json.dump(new_intents, outfile)
with open('json_data.json') as json_file:
    data = json.load(json_file)
print(data['intents'])

I got a TypeError: string indices must be integers
I am not sure what’s the problem and how to fix it.

Asked By: Ulffer

||

Answers:

You’re double-encoding the Json. Drop the first new_intents = json.dumps(intents, indent=4) and it should work:

# remove this line:
# new_intents = json.dumps(intents, indent=4)

with open('json_data.json', 'w') as outfile:
    json.dump(intents, outfile, indent=4)   # <-- change new_intents to intents

with open('json_data.json') as json_file:
    data = json.load(json_file)

print(data['intents'])
Answered By: Andrej Kesely

as shown in the following tutorial:
https://www.geeksforgeeks.org/how-to-convert-python-dictionary-to-json/
the dumps function is used to convert a dictionary into json and also there is dump which writes dictionary as json directly into the file, you are using them both which is not required and is causing your program to first convert the dict into json and then the json into json(which corrupts it), so the solution is:

import json

intents = {"intents": [
    {"tag": "greeting",
     "patterns": ["Hi", "Hey", "Is anyone there?", "Hello", "Hay"],
     "responses": ["Hello", "Hi", "Hi there"]
    },
    {"tag": "goodbye",
     "patterns": ["Bye", "See you later", "Goodbye"],
     "responses": ["See you later", "Have a nice day", "Bye! Come back again"]
    },
    ...
    {"tag": "createaccount",
    "patterns": ["I need to create a new account", "how to open a new account", "I want to create an account", "can you create an account for me", "how to open a new account"],
    "responses": ["You can just easily create a new account from our web site", "Just go to our web site and follow the guidelines to create a new account"]
    },
    {"tag": "complaint",
    "patterns": ["have a complaint", "I want to raise a complaint", "there is a complaint about a service"],
    "responses": ["Please provide us your complaint in order to assist you", "Please mention your complaint, we will reach you and sorry for any inconvenience caused"]
    }
]
}
with open('json_data.json', 'w') as outfile:
    json.dump(intents, outfile)
with open('json_data.json') as json_file:
    data = json.load(json_file)
print(data['intents'])

which doesn’t use both the dump() and dumps() function.

Answered By: Muhammad Mirab Br.
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.