Reputation: 525
Sorry for asking a repeating question, but I am stuck here. In my code, a variable contains the following data:
{
"AlarmName":"ZDR ALARAM - 404 - ERROR",
"AlarmDescription":"This will check the 404 Errors in ZDR server",
"Trigger":{
"MetricName":"ZDR4dor",
"Namespace":"ZDRLdics",
"StatisticType":"Statistic",
"Statistic":"SUM",
"Unit":null,
"Dimensions":[ ],
"Period":60,
"EvaluationPeriods":1,
"ComparisonOperator":"GreatergrfdgsThreshold",
"Threshold":0,
"TreatMissingData":"",
"EvaluatxcbleCountPercentile":""
}
}
I need to get AlarmName
. I am using var['AlarmName']
, but it shows the error index must be an integer not string. When I use var[0]
it shows the output
{
only,
The object type is <type 'unicode'>
How do I get the exact value of the key 'AlarmName'?
Upvotes: 0
Views: 31
Reputation: 7186
Currently your var
is just a (unicode) string containing the information. You need to parse it, so it is a dictionary object, which can handle the item access. The easiest way is to use the json
module, since your string seems to be a valid json
object:
import json
var = """
{
"AlarmName":"ZDR ALARAM - 404 - ERROR",
"AlarmDescription":"This will check the 404 Errors in ZDR server",
"Trigger":{
"MetricName":"ZDR4dor",
"Namespace":"ZDRLdics",
"StatisticType":"Statistic",
"Statistic":"SUM",
"Unit":null,
"Dimensions":[ ],
"Period":60,
"EvaluationPeriods":1,
"ComparisonOperator":"GreatergrfdgsThreshold",
"Threshold":0,
"TreatMissingData":"",
"EvaluatxcbleCountPercentile":""
}
}
"""
var = json.loads(var)
print(var['AlarmName'])
# ZDR ALARAM - 404 - ERROR
Upvotes: 1