As it stands, the structure of your data is apparently inconsistent. As commented by Sidon, if used JSON, a format that would make much 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 that the information is in the desired format or that you have no way to 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’ll have the exit:
{dado=informação_dado}
To get only the content after the =
, we can find the content within 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’ll see he’s worth slice(6, -1, None)
, that is, return the positions of the string from index 6 to penultimate character.
dado = data.get("informacao2")[part]
In this way, dado
worth informação_dado
.
See working on Repl.it.
whereas informacao2
be something like:
"{dado=informação_dado, dado2=informação_dado2, dado3=informação_dado3}"
You can get all the values of dados
through regular expression:
groups = re.findall(r"(?:dado\d*\=)(.*?)(?:[,}])", data.get("informacao2"))
In this case, by print(groups)
, we shall have:
['informação_dado', 'informação_dado2', 'informação_dado3']
That is, a list of all the data from string. To get the last amount, just do groups[-1]
.
Is JSON exactly the way you put it? Why is the data there a string and is not in a JSON format. To get this information you will have to handle the string, with regex, for example.
– Woss
It’s like this, I get it, there’s no other way to capture that value?
– DaniloAlbergardi