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
  Errors using json file RonR 5 956 Oct-28-2025, 03:00 PM
Last Post: buran
  Convert JSON to CSV Linuxdesire 6 8,022 Oct-15-2025, 12:05 AM
Last Post: Pedroski55
  Convert Json to table formathttps://python-forum.io/thread-38313.html python_student 3 18,705 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,250 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  encrypt data in json file help jacksfrustration 1 3,569 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 16,800 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 13,055 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  parse json field from csv file lebossejames 4 3,004 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Convert File to Data URL michaelnicol 3 4,128 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Convert np Array A to networkx G IanAnderson 2 2,265 Jul-05-2023, 11:42 AM
Last Post: IanAnderson

Forum Jump:

User Panel Messages

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