In the below-nested dictionary, I want term present in "fields" value in "field_name" string.
response = {
"indices": [
"device",
],
"fields": {
"hostname": {
"text": {
"type": "text",
"searchable": true,
"aggregatable": false
}
}
}
}
My code-
for field in response['fields'][field]:
if field['fields'][field] == field_name:
Error-
if field['fields'] == field_name:
TypeError: string indices must be integers
Expected Output-
fieldname=hostname
In below example dictionary-I am able to extract "name" and "type" using below code.It works fine..
{
"name": "hostname",
"type": "string",
"esTypes": [
"text"
],
"searchable": true,
"aggregatable": false,
"readFromDocValues": false
}
Code-
for field in response['fields']:
if field['name'] == field_name:
d_type = field['esTypes']
CodePudding user response:
Can you explain what you're hoping to achieve here in more detail? If you are trying to extract the data from fields, you can access it via
response['fields']
Or if you want to get the data from hostname, you can access it via
response['fields']['hostname']
Happy to help further if this isn't what you intended.
CodePudding user response:
Try to use this
for field in response['fields']:
if field == field_name:
d_type = response['fields'][field]['esTypes']
field
- key of dictionary
If field_name
is 'hostname', then response['fields'][field]
inside loop get dictionary:
"text": {
"type": "text",
"searchable": True,
"aggregatable": False
}