Python Forum
convert a json file to a python dictionnary of array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert a json file to a python dictionnary of array
#1
Hi there, i m a newbee so excuse if my question looks dummy.

so i have a json file which look like this

Output:
[{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'}, {'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'}, {'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]
and i want to read this json file in order to convert it into a dictionnary of array which looks like

Output:
[{'_id': [ '1', '2','3']}, {'date': [ '2019-09-07', '2019-09-08','2019-09-09']}, {'name': [ 'abi', 'allo','ali']}, {'value': [ '0', '3','0']}, {'unit': [ '°C', '°F','°C']},]
Thank you in advance
Reply
#2
what have you tried? Note that so called json that you have is not valid one. it looks like already converted to JSON object in python
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Sep-10-2019, 10:26 AM)buran Wrote: what have you tried? Note that so called json that you have is not valid one. it looks like already converted to JSON object in python

thanks Buran,
i find out this solution
from collections import defaultdict

data = [{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'},
{'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'},
{'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]

result = defaultdict(list)
for i in data:
    for k, v in i.items():
        result[k].append(v)
print(result)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Json to table format python_student 4 14,174 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,010 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  encrypt data in json file help jacksfrustration 1 2,140 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 9,459 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 8,848 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  parse json field from csv file lebossejames 4 1,955 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Convert File to Data URL michaelnicol 3 2,580 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Convert np Array A to networkx G IanAnderson 2 1,471 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Python Script to convert Json to CSV file chvsnarayana 8 4,601 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 5,198 Apr-06-2023, 11:15 AM
Last Post: AlphaInc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020