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
  encrypt data in json file help jacksfrustration 1 195 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 803 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,875 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  parse json field from csv file lebossejames 4 729 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Convert File to Data URL michaelnicol 3 1,151 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Convert np Array A to networkx G IanAnderson 2 675 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Python Script to convert Json to CSV file chvsnarayana 8 2,499 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,099 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,790 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,407 Jan-29-2023, 03:59 PM
Last Post: eyavuz21

Forum Jump:

User Panel Messages

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