0

I have some json that I have converted to a list in Python like this:

jsonText = '[{"stationID":"ABCD1234","obsTimeUtc":"2021-05-04T11:53:04Z","obsTimeLocal":"2021-05-04 21:53:04","neighborhood":"HelloWorld","softwareType":"SoftwareHelloWorld","country":"Hello","solarRadiation":0.0,"lon":1234.1234,"realtimeFrequency":null,"epoch":12345678,"lat":1234.1234,"uv":0.0,"winddir":1234,"humidity":1234,"qcStatus":1,"imperial":{"temp":57,"heatIndex":57,"dewpt":56,"windChill":57,"windSpeed":0,"windGust":0,"pressure":29.95,"precipRate":0.07,"precipTotal":0.21,"elev":56}}]'
listText = js.loads(jsonText)
print('--NEW FORMAT--')
print(listText)

Which returns this list:

[{'stationID': 'ABCD1234', 'obsTimeUtc': '2021-05-04T11:53:04Z', 'obsTimeLocal': '2021-05-04 21:53:04', 'neighborhood': 'HelloWorld', 'softwareType': 'SoftwareHelloWorld', 'country': 'Hello', 'solarRadiation': 0.0, 'lon': 1234.1234, 'realtimeFrequency': None, 'epoch': 12345678, 'lat': 1234.1234, 'uv': 0.0, 'winddir': 1234, 'humidity': 1234, 'qcStatus': 1, 'imperial': {'temp': 57, 'heatIndex': 57, 'dewpt': 56, 'windChill': 57, 'windSpeed': 0, 'windGust': 0, 'pressure': 29.95, 'precipRate': 0.07, 'precipTotal': 0.21, 'elev': 56}}]

However I don't want the keys in the list (stationID:. obsTimeUtc: etc.), only the values so that it would look more like this:

[["ABCD1234","2021-05-04T11:53:04Z","2021-05-04 21:53:04","HelloWorld","SoftwareHelloWorld","Hello",0.0,1234.1234,"null",12345678,1234.1234,0.0,1234,1234,1,57,57,56,57,0,0,29.95,0.07,0.21,56]]

How do I remove the "keys" in the list and just keep the values?

3 Answers 3

1

I would do it with a list comprehension. This will handle the case when there are multiple dicts in the list structure:

data = [list(d.values()) for d in listText]

To handle recursive dicts I would use a generator:

def flatten(listText):
    def _flatten(item):
        if isinstance(item, dict):
            for value in item.values():
                yield from _flatten(value)
        else:
            yield item

    return [list(_flatten(d)) for d in listText]
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is that if some value is also a dict, this approach won´t work. You need a recursive solution, as I did in my answer.
Based on the result example given by the OP, the solution definitely requieres handling nested dicts
This works almost perfectly however it still returns the list with keys after from 'imperial:'- I believe this is because it is nested. Do you know how to solve this?
A one dimensional list is all that needs to be returned :)
@BenHarvey Ah I missed that. Updated.
|
1

list(listText[0].values()) will make a list of the values.

Comments

1
def listFromDict(d):
    resultList = []
    for k in d.keys():
        if isinstance(d[k], dict):
            resultList.extend(listFromDict(d[k]))
        else:
            resultList.append(d[k])
        
    return resultList

result = []
for e in listText:
    result.append(listFromDict(e))

print(result)
    

3 Comments

In this case is "d" a dictionary or would it be the listText from my original code
d is just a variable in the listFromDict function. Your listText is used below in the code
I just realized that my formatting was wrong, now it is indented properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.