1
data = ['{"osc":{"version":"1.0"}}']

or

data =  ['{"device":{"network":{"ipv4_dante":{"auto":"testing"}}}}']

From the code above, I only get random outputs, but I need to get the last value i.e "1.0" or "testing" and so on.

I always need to get the last value. How can I do it using python?

2
  • the last element of the dictionary. That is "1.0" and "testing" Commented Mar 27, 2018 at 7:54
  • @syam.k When you edit, edit everything i.e. code formatting. grammar, and style, if you can. Commented Mar 27, 2018 at 7:56

2 Answers 2

2

Dictionaries have no "last" element. Assuming your dictionary doesn't branch and you want the "deepest" element, this should work:

import json
data =  ['{"device":{"network":{"ipv4_dante":{"auto":"testing"}}}}']

obj = json.loads(data[0])
while isinstance(obj, dict):
    obj = obj[list(obj.keys())[0]]
print(obj)
Sign up to request clarification or add additional context in comments.

Comments

0

This should work -

import ast
x = ast.literal_eval(data[0])
while(type(x)==dict):
    key = x.keys()[0]
    x = x.get(key)
print(x)

Comments

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.