As it stands, the structure of its data is apparently inconsistent. As commented by Sidon, if you used JSON, a format that would make a lot more sense would be:
{
"informacao1": "valor_informação1",
"informacao2": {
"dado": "informação_dado"
}
}
If you have autonomy over the code that generates this JSON, I would recommend that you make this change. Otherwise, assuming the information is in the desired format or you can not modify it, you can get the information like this:
Consider the input data:
content = '''{
"informacao1": "valor_informação1",
"informacao2": "{dado=informação_dado}"
}'''
Analyzing JSON, converting to a Python object:
data = json.loads(content)
If we do:
print(data.get("informacao2"))
We will have the output:
{dado=informação_dado}
To get only content after =
, we can find the index inside the string of this character and return the part of it from this position to the penultimate character:
part = slice(data.get("informacao2").index("=") + 1, -1)
In this case, if you do print(part)
, you will see that it is slice(6, -1, None)
, that is, it will return the positions of string between index 6 to the penultimate character.
dado = data.get("informacao2")[part]
In this way, dado
will be informação_dado
.
See working at Repl.it .
Considering that informacao2
is something like:
"{dado=informação_dado, dado2=informação_dado2, dado3=informação_dado3}"
You can get all values of dados
through regular expression:
groups = re.findall(r"(?:dado\d*\=)(.*?)(?:[,}])", data.get("informacao2"))
In this case, when doing print(groups)
, we will have:
['informação_dado', 'informação_dado2', 'informação_dado3']
That is, a list with all the data of the string . To get the last value, just do groups[-1]
.