0

I am try to use python to extract value, but I found a weird result. Following is part of my json variable

"weatherElement": [
 {
  "elementName": "ELEV",
  "elementValue": {
   "value": "20.0"
  }
 },
 {
  "elementName": "TEMP",
  "elementValue": {
   "value": "25.0"
  }
 },
 {
  "elementName": "D_TNT",
  "elementValue": {
   "value": "2019-11-22T02:10:00+08:00"
  }
 }
],

and following code is correct for getting value 25.0 which is temperature

for unit in data['records']['location']:
    # print(type(unit))  <---- output <dict>
    if unit['stationId'] == 'C0V490':
        for wea_unit in unit['weatherElement']: # unit['weatherElement'] is list
            if wea_unit['elementName'] == 'TEMP':
                print(type(wea_unit['elementValue'])) # is str
                return wea_unit['elementValue']

My question is why type(wea_unit['elementValue']) is str?

I think it should be dict and I should use wea_unit['elementValue']['value'] to get '25.0', but it is wrong. Are there anyone know what mistake I made? Thanks!

edit:

following is example code which can run directly

import json
def parse_json_to_dataframe(data):
    for unit in data['records']['location']:
        # print(type(unit))
        if unit['stationId'] == 'C0A560':
            for wea_unit in unit['weatherElement']: # unit['weatherElement'] is list
                if wea_unit['elementName'] == 'TEMP':
                    print(type(wea_unit['elementValue']))
                    return wea_unit['elementValue']
v = {"success":"true",
"result": {"resource_id": "O-A0001-001", 
"fields": [{"id": "lat", "type": "Double"},
{"id": "lon", "type": "Double"}, 
{"id": "locationName", "type": "String"}, 
{"id": "stationId", "type": "String"}, 
{"id": "description", "type": "String"}, 
{"id": "elementName", "type": "String"}, 
{"id": "elementValue", "type": "Double"}, 
{"id": "parameterName", "type": "String"}, 
{"id": "parameterValue", "type": "String"}]}, # result end
"records": {"location": [{"lat": "24.778333", 
"lon": "121.494583",
"locationName": "福山", 
"stationId": "C0A560", 
"time": {"obsTime": "2019-11-22 22:00:00"}, 
"weatherElement": [
{"elementName": "ELEV", "elementValue": "405.0"}, 
{"elementName": "WDIR", "elementValue": "0"}, 
{"elementName": "WDSD", "elementValue": "0.0"}, 
{"elementName": "TEMP", "elementValue": "19.6"}], 
"parameter": [
{"parameterName": "CITY_SN", "parameterValue": "06"}, 
{"parameterName": "TOWN_SN", "parameterValue": "061"}]}]}}
temp = parse_json_to_dataframe(v)
print(temp)
Steven
  • 811
  • 4
  • 23
  • it gives me that it's dict!, maybe the you didn't parse the string you have, follow the solution here: https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary – Minions Nov 22 '19 at 14:23
  • @Ghanem I doubt that would be the issue, since the code would error out on `data['records']['location']` already. OP, please create a [mre] as right now it doesn't seem reproducible. Also, what are you getting in `return`? – r.ook Nov 22 '19 at 14:27
  • @r.ook , I meant maybe the last part inside "elementValue". – Minions Nov 22 '19 at 14:30
  • @Ghanem I get 25.0 for return value – Steven Nov 22 '19 at 14:34
  • @Ghanem Based on the data provided `elementValue` is definitely a `dict` though. The entire data was either taken in as a string (would fail at the aforementioned spot) or `type(wea_unit['elementValue'])` would return `dict`, it can't be half string and half parsed. Unless, OP has an `elementValue` besides the shown data that is actually a `str`. – r.ook Nov 22 '19 at 14:34
  • Actually, the json variable I gave is partial. – Steven Nov 22 '19 at 14:37
  • @Steven then you have an actual `str` type in there somewhere within `elementValue`. And it will raise an error if you try to retrieve `wea_unit['elementValue']['value']` at that iteration. Again, creating a [mre] would help both you and us step through the problem. – r.ook Nov 22 '19 at 14:38
  • @r.ook hey, I just update an example code which can run directly. Hope you can give me some suggestions, thanks! – Steven Nov 22 '19 at 15:17
  • @Steven , the first json file you provide us is different than the last one. In the first one `elementValue` is dict you're right `"elementValue": {"value": "25.0"}`, but not in the second one. `{"elementName": "TEMP", "elementValue": "19.6"}` – Minions Nov 22 '19 at 19:15
  • Your new sample is showing that `elementValue` is indeed a `str` type. The code is running exactly as it's expected to. If you wanted to use it as a float value, do `float(wea_unit['elementValue'])` – r.ook Nov 22 '19 at 20:10
  • yes, I finally found both examples are different, first one is got from .json, and last one is get from python `.json()` method. So it seems like python transfer `elementValue": {"value": "25.0"}` to `{"elementName": "TEMP", "elementValue": "19.6"}` right? – Steven Nov 22 '19 at 20:53

0 Answers0