-2

I need a way to read multiple curly brackets in python. I have a test json that has multiple layers of curly brackets and I need to get to the deepest layer. My test json: my json (I need it to return "name". My code isn't working, but this is what it looks like (not all of it, just the important parts.)

import json 
    
with open(args.mc_path + "launcher_profiles.json", "r+") as profiles:
    data = json.load(profiles)

test = data["profiles": { "053abfb0fb7451c291b9149ea7df88c8": {"name"}}]
print(test)
Pogman69
  • 3
  • 2
  • 1
    You need to use the standard Python dictionary way to access data, in your case `data["profiles"]["053abfb0fb7451c291b9149ea7df88c8"]["name"]` – h0r53 Aug 24 '22 at 19:37
  • Part of building a [mre] is eliminating details that aren't actually pertinent to the question. The fact that your data structure was created by loading a JSON file doesn't change how you access it -- if you had that data hardcoded in your Python source code and it was never represented as JSON at all, the access method would still be the same. – Charles Duffy Aug 24 '22 at 19:44

1 Answers1

0

You can index on nested dictionaries multiple times to get the nested value. In your case that would look like:

test = data["profiles"]["053abfb0fb7451c291b9149ea7df88c8"]["name"]
tiger
  • 225
  • 1
  • 9