Python JSON assign data from API

Question:

I have a py file to read data from WordPress API and pass values to another fields of other API. When values are singles, i have no problem, but i don’t know how make that:
When i read one field from the API, the states values, comes with code instead the text value. For example, when the text value in WordPress is Barcelona, returns B, and i’ll need that the value returned will be Barcelona.
One example of code with simple fields values:

oClienteT["Direcciones"] = []
oClienteT["Telefono"] = oClienteW["billing"]["phone"]
oClienteT["NombreFiscal"] = oClienteW["first_name"] " " oClienteW["last_name"]
oClienteT["Direcciones"].append( {
"Codigo" : oClienteW["id"],
"Nombre" : oClienteW["billing"]["first_name"],
"Apellidos" : oClienteW["billing"]["last_name"],
"Direccion" : oClienteW["billing"]["address_1"],
"Direccion2" : oClienteW["billing"]["address_2"],
"Poblacion" : oClienteW["billing"]["state"],
"Provincia" : oClienteW["billing"]["city"]
})

When billing city is Madrid and billing state is madrid, WordPress returns Madrid and M
I need tell thst when Madrid, returns Madrid, and so on.

Asked By: oraculo

||

Answers:

Make sure to convert to a JSON object before accessing fields (data = json.loads(json_str))

response = { "billing": { "address_1": "C/GUSTAVO ADOLFO BECQUER, 4", "city": "SEVILLA", "state": "SE"}}

print(response["billing"].get("address_1", None))
Answered By: Januka samaranyake

I’ve just got solved it:

def initProvincias(self):
self.aProvincias['C']  = 'A Coruña'
self.aProvincias['VI']  = 'Álava'
def getProvincia(self , sCod ):
if not sCod in self.aProvincias:
_logger.info("PROVINCIA NO ENCONTRADA "+str(sCod))
return ""
return self.aProvincias[sCod]

"Provincia"     : self.getProvincia( oClienteW["shipping"]["state"] ),
Answered By: oraculo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.