this is JSON text:
{
"result":"success",
"clientid":"None",
"serviceid":"1232",
"pid":"None",
"domain":"None",
"totalresults":1,
"startnumber":0,
"numreturned":1,
"products":{
"product":[
{
"id":3212,
"clientid":2025,
"orderid":3285,
"ordernumber":34352,
"pid":2,
"regdate":"2022-08-10",
"name":"P2",
"translated_name":"P2",
"groupname":"VPS",
"translated_groupname":"VPS",
"domain":"Mi",
"dedicatedip":"123.456.345:22600",
"serverid":0,
"servername":"",
"serverip":"None",
"serverhostname":"None",
"suspensionreason":"",
"firstpaymentamount":"1.00",
"recurringamount":"1.00",
"paymentmethod":"Cryptocurrencies",
"paymentmethodname":"Cryptocurrencies",
"billingcycle":"Monthly",
"nextduedate":"2022-09-10",
"status":"Active",
"username":"Admin",
"password":"ABCD",
"subscriptionid":"",
"promoid":0,
"overideautosuspend":0,
"overidesuspenduntil":"0000-00-00",
"ns1":"Prefix1",
"ns2":"Prefix2",
"assignedips":"123.345.235.66:21600",
"notes":"example",
"diskusage":0,
"disklimit":0,
"bwusage":0,
"bwlimit":0,
"lastupdate":"0000-00-00 00:00:00",
"customfields":{
"customfield":[
{
"id":4,
"name":"Your Operating System",
"translated_name":"Your Operating System",
"value":"Windows 10 64bit"
},
{
"id":5,
"name":"Location",
"translated_name":"Location",
"value":" Falkenstein Germany"
},
{
"id":142,
"name":"ID",
"translated_name":"ID",
"value":"1234"
}
]
},
"configoptions":{
"configoption":[
]
}
}
]
}
}
this is my code:
data = {
'identifier':identifier,
'secret':secret,
'action': 'GetClientsProducts',
'serviceid':service_id,
'responsetype':'json',
}
r = requests.post(url = API_ENDPOINT, data = data)
data = r.json()
for order in data["products"]["product"]:
name_of_server = order['name']
print(order['name'])
for item in order["customfields"]["customfield"]:
if item["name"] in 'Your Operating System':
OS_of_Server = item["value"]
print(item["value"])
if item["name"] in 'Location':
print(item['value'])
...
result:
P2
Windows 10 64bit
I want get value of name: Location , should be: "Falkenstein Germany"
CodePudding user response:
I used the following code
your json response
import json
a = """{
"result":"success",
"clientid":"None",
"serviceid":"1232",
"pid":"None",
"domain":"None",
"totalresults":1,
"startnumber":0,
"numreturned":1,
"products":{
"product":[
{
"id":3212,
"clientid":2025,
"orderid":3285,
"ordernumber":34352,
"pid":2,
"regdate":"2022-08-10",
"name":"P2",
"translated_name":"P2",
"groupname":"VPS",
"translated_groupname":"VPS",
"domain":"Mi",
"dedicatedip":"123.456.345:22600",
"serverid":0,
"servername":"",
"serverip":"None",
"serverhostname":"None",
"suspensionreason":"",
"firstpaymentamount":"1.00",
"recurringamount":"1.00",
"paymentmethod":"Cryptocurrencies",
"paymentmethodname":"Cryptocurrencies",
"billingcycle":"Monthly",
"nextduedate":"2022-09-10",
"status":"Active",
"username":"Admin",
"password":"ABCD",
"subscriptionid":"",
"promoid":0,
"overideautosuspend":0,
"overidesuspenduntil":"0000-00-00",
"ns1":"Prefix1",
"ns2":"Prefix2",
"assignedips":"123.345.235.66:21600",
"notes":"example",
"diskusage":0,
"disklimit":0,
"bwusage":0,
"bwlimit":0,
"lastupdate":"0000-00-00 00:00:00",
"customfields":{
"customfield":[
{
"id":4,
"name":"Your Operating System",
"translated_name":"Your Operating System",
"value":"Windows 10 64bit"
},
{
"id":5,
"name":"Location",
"translated_name":"Location",
"value":" Falkenstein Germany"
},
{
"id":142,
"name":"ID",
"translated_name":"ID",
"value":"1234"
}
]
},
"configoptions":{
"configoption":[
]
}
}
]
}
}
"""
code to get values
j_obj = json.loads(a)
for i in j_obj['products']['product']:
print(i['name'])
for j in i['customfields']['customfield']:
if j['name']=='Location':
print(j['value'])
if j['name']=='Your Operating System':
print(j['value'])