Python JSON найти родительский узел на основе ключа
У меня есть следующая функция, которая позволяет мне искать через json и возвращать значение искомого ключа.
Тем не менее, я пытаюсь найти фактический parentNode ключа, который ищем, и нахожу это немного сложным.
def bar(somejson, key):
def val(node):
# Searches for the next Element Node containing Value
e = node.nextSibling
while e and e.nodeType != e.ELEMENT_NODE:
e = e.nextSibling
return (e.getElementsByTagName('string')[0].firstChild.nodeValue if e
else None)
# parse the JSON as XML
foo_dom = parseString(xmlrpclib.dumps((json.loads(somejson),)))
# and then search all the name tags which are P1's
# and use the val user function to get the value
return [val(node) for node in foo_dom.getElementsByTagName('name')
if node.firstChild.nodeValue in key]
Я пытался использовать parentNode вместо firstChild, но он не работает. Есть идеи, где я иду не так?