Home > database >  Python: Converting data to appropriate dictionary format
Python: Converting data to appropriate dictionary format

Time:07-05

I filtered and processed some data and I got it in the following format:

{'2020-04-20': [('EUR', 34.02), ('USD', 30.18), ('AWG', 24.44), ('GPB', 20.68)], 
 '2020-04-25': [('EUR', 16.88), ('USD', 15.06), ('AWG', 12.17), ('GPB', 10.4)], 
 '2020-04-27': [('EUR', 17.14), ('GPB', 10.28), ('USD', 7.58), ('AWG', 5.06), ('CZK', 2.44)]
...
}

Now, I want to save it to the JSON format, which should look this way:

json_data = {
  "filtered_data": [
    {
      "data": "2015-01-04",
      "currency": "EUR",
      "value": 34.02,
    },
    {
       "data": "2015-01-04",
       "currency": "USD",
       "value": 30.18,
    },
    {
       "data": "2015-01-04",
       "currency": "AWG",
       "value": 24.44,
    },

    ...
  ]
}

I'd like to have each currency and value with date (even if data is the same in some cases).

However, I managed to do only this:

json_data = {"filtered_data": []}

for key, val in filtered_data.items():
    for v in filtered_data.values():
        temp_dict = {"timestamp": key, "currency": v[0], "value": v[1]}
        json_data["filtered_data"].append(temp_dict)

json_data = json.dumps(json_data, indent=2)

Output:

{
  "filtered_data": [
    {
      "timestamp": "2020-04-20",
      "currency": [
        "EUR",
        34.02
      ],
      "value": [
        "USD",
        30.18
      ]
}...

CodePudding user response:

You can perform a traversal on the first dictionary with a nested loop, appending all values to the resulting out variable, inside "filtered_data":

d = {'2020-04-20': [('EUR', 34.02), ('USD', 30.18), ('AWG', 24.44), ('GPB', 20.68)], 
 '2020-04-25': [('EUR', 16.88), ('USD', 15.06), ('AWG', 12.17), ('GPB', 10.4)]}

out = {"filtered_data": []}
for i in d:
  for j in d[i]:
    out["filtered_data"].append({"data": i, "currency": j[0], "value": j[1]})
print(out)

Output:

{'filtered_data': [{'data': '2020-04-20', 'currency': 'EUR', 'value': 34.02}, {'data': '2020-04-20', 'currency': 'USD', 'value': 30.18}, {'data': '2020-04-20', 'currency': 'AWG', 'value': 24.44}, {'data': '2020-04-20', 'currency': 'GPB', 'value': 20.68}, {'data': '2020-04-25', 'currency': 'EUR', 'value': 16.88}, {'data': '2020-04-25', 'currency': 'USD', 'value': 15.06}, {'data': '2020-04-25', 'currency': 'AWG', 'value': 12.17}, {'data': '2020-04-25', 'currency': 'GPB', 'value': 10.4}]}

CodePudding user response:

You can first iterate over dict then use for-loop on all value with repeated date:

import json
res = [{'data':k, 'currancy':a, 'value':b} for k,v in dct.items() for a,b in v]
json_data = {"filtered_data": res}
print(json.dumps(json_data))

Output:

{"filtered_data": [{"data": "2020-04-20", "currancy": "EUR", "value": 34.02}, {"data": "2020-04-20", "currancy": "USD", "value": 30.18}, {"data": "2020-04-20", "currancy": "AWG", "value": 24.44}, {"data": "2020-04-20", "currancy": "GPB", "value": 20.68}, {"data": "2020-04-25", "currancy": "EUR", "value": 16.88}, {"data": "2020-04-25", "currancy": "USD", "value": 15.06}, {"data": "2020-04-25", "currancy": "AWG", "value": 12.17}, {"data": "2020-04-25", "currancy": "GPB", "value": 10.4}, {"data": "2020-04-27", "currancy": "EUR", "value": 17.14}, {"data": "2020-04-27", "currancy": "GPB", "value": 10.28}, {"data": "2020-04-27", "currancy": "USD", "value": 7.58}, {"data": "2020-04-27", "currancy": "AWG", "value": 5.06}, {"data": "2020-04-27", "currancy": "CZK", "value": 2.44}]}

CodePudding user response:

I think you want something like this:

j = {'2020-04-20': [('EUR', 34.02), ('USD', 30.18), ('AWG', 24.44), ('GPB', 20.68)], 
 '2020-04-25': [('EUR', 16.88), ('USD', 15.06), ('AWG', 12.17), ('GPB', 10.4)], 
 '2020-04-27': [('EUR', 17.14), ('GPB', 10.28), ('USD', 7.58), ('AWG', 5.06), ('CZK', 2.44)]
}

json_data = {"filtered_data": []}

for k,v in j.items():
    for i in v:
        temp_dict = {"timestamp": k, "currency": v[0], "value": v[1]}
        json_data["filtered_data"].append(temp_dict)
        
print(json_data)

{'filtered_data': [{'timestamp': '2020-04-20', 'currency': ('EUR', 34.02), 'value': ('USD', 30.18)}, {'timestamp': '2020-04-20', 'currency': ('EUR', 34.02), 'value': ('USD', 30.18)}, {'timestamp': '2020-04-20', 'currency': ('EUR', 34.02), 'value': ('USD', 30.18)}, {'timestamp': '2020-04-20', 'currency': ('EUR', 34.02), 'value': ('USD', 30.18)}, {'timestamp': '2020-04-25', 'currency': ('EUR', 16.88), 'value': ('USD', 15.06)}, {'timestamp': '2020-04-25', 'currency': ('EUR', 16.88), 'value': ('USD', 15.06)}, {'timestamp': '2020-04-25', 'currency': ('EUR', 16.88), 'value': ('USD', 15.06)}, {'timestamp': '2020-04-25', 'currency': ('EUR', 16.88), 'value': ('USD', 15.06)}, {'timestamp': '2020-04-27', 'currency': ('EUR', 17.14), 'value': ('GPB', 10.28)}, {'timestamp': '2020-04-27', 'currency': ('EUR', 17.14), 'value': ('GPB', 10.28)}, {'timestamp': '2020-04-27', 'currency': ('EUR', 17.14), 'value': ('GPB', 10.28)}, {'timestamp': '2020-04-27', 'currency': ('EUR', 17.14), 'value': ('GPB', 10.28)}, {'timestamp': '2020-04-27', 'currency': ('EUR', 17.14), 'value': ('GPB', 10.28)}]}

  • Related