Hi,

I have a nested dictionary like the following how do I loop through such dictionary ,Basically I am looking to get the value of key "name" from the following dictionary

Any help is appreciated,

[{'resource': 
 ['{"metadata":
 {"type": "directory",
 "size": "8"}, "name": "default", 
 "links": 
       {"link": ["{\\"href\\": \\"https://my.server.comdefault\\", 
       \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://my.server.com/default\\", 
       \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://my.server.com//default\\",
       \\"rel\\": \\"webdav\\"}"]},
       "uri": "https://my.server.com//resources/default"}', 
'{"metadata": 
 {"type": "directory",
  "size": "2"}, 
  "name": "estolume",
"links": 
      {"link": ["{\\"href\\": \\"https://my.server.com/lume\\",
      \\"rel\\":\\"self\\"}", 
      "{\\"href\\": \\"https://my.server.com/tolme\\", 
      \\"rel\\": \\"edit\\"}", 
      "{\\"href\\": \\"https://my.server.com/lume\\", 
      \\"rel\\": \\"webdav\\"}"]},
      "uri": "https://my.server.com/lume"}',  
'{"metadata": 
 {"type": "directory",
  "size": "2"}, 
  "name": "creatingnew", 
  "links":
      {"link": ["{\\"href\\": \\"https://my.server.com//resources/creatingnew\\", 
      \\"rel\\": \\"self\\"}",
      "{\\"href\\": \\"https://my.server.com//resources/creatingnew\\",
      \\"rel\\": \\"edit\\"}", 
      "{\\"href\\": \\"https://my.server.com/newuserKey/creatingnew\\",
      \\"rel\\": \\"webdav\\"}"]}, 
      "uri": "https://my.server.com//resources/creatingnew"}']

something is wrong with the actual dictionary, when i put it into python i get errors along the lines of
SyntaxError: EOL while scanning single-quoted string

and i tried for a few minutes but i couldnt get it to work, the "" marks were all out of place.

Thanks for looking into this that is not a complete dictionary I have just copied and pasted part of it, just wanted to get some idea how I wil do that
Do you want me post the complete dict.?

Yeah it would be nice to have the dictionary Exactly as it is in your program.. and also for readability just wrap it in

Your code here

but only have one forward slash

OK here it is...

resource 	{'resource': ['{"metadata": {"type": "directory", "size": "8"}, "name": "home", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/home\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/home\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/home\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/home"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "estolume", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/estolume\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/estolume\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/estolume\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/estolume"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "creatingnew", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/creatingnew\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/creatingnew\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/creatingnew\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/creatingnew"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "test", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/test\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/test\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/test\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/test"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "NewFolder", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/NewFolder\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/NewFolder\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/NewFolder\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/NewFolder"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "NewTest", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/NewTest\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/NewTest\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/NewTest\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/NewTest"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "newTestVolume", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/newTestVolume\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/newTestVolume\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/newTestVolume\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/newTestVolume"}']}

I see a problem here. Firstly have a look at the start, there is an ' mark, this makes the value of resource a string forever and no dictionary. So resource equals all this:

}, "uri": "https://myserver.com//users/userkey/values/home"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "estolume", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/estolume\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/estolume\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/estolume\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/estolume"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "creatingnew", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/creatingnew\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/creatingnew\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/creatingnew\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/creatingnew"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "test", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/test\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/test\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/test\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/test"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "NewFolder", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/NewFolder\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/NewFolder\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/NewFolder\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/NewFolder"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "NewTest", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/NewTest\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/NewTest\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/NewTest\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/NewTest"}', '{"metadata": {"type": "directory", "size": "2"}, "name": "newTestVolume", "links": {"link": ["{\\"href\\": \\"https://myserver.com//users/userkey/values/newTestVolume\\", \\"rel\\": \\"self\\"}", "{\\"href\\": \\"https://myserver.com//users/userkey/values/newTestVolume\\", \\"rel\\": \\"edit\\"}", "{\\"href\\": \\"https://myserver.com/userkey/newTestVolume\\", \\"rel\\": \\"webdav\\"}"]}, "uri": "https://myserver.com//users/userkey/values/newTestVolume"}']

And thats not what we want to try taking the ' key out of the second character in the last bit and that should make more problems but if you solve the correct way around that the quotation marks should go then you might get a valid dictionary

Thanks a lot for pointing this out,let try and correct this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.