Python Forum
from Json Time Serie file to python dictionnary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from Json Time Serie file to python dictionnary
#1
Hi, i have a preoccupation, i know the process to follow but not exactly how to implement it.

so i have a json file which looks like

json = [{"signal_name":"X", "signal_value":"valueX1", "time"="timeX1","par"="id1"}, 

{"signal_name":"Y", "signal_value":"valueY1", "time":"timeY1","par":"id2"},

{"signal_name":"Z", "signal_value":"valueZ1", "time":"timeZ1","par":"id3"},

{"signal_name":"X", "signal_value":"valueX2", "time":"timeX2","par":"id5"}]
and so many other lines...

so the first thing i want to do is store as array the signal name with their different values, and the corresponding time value as follow
Array_nameX = [ "valueX1", "valueX2" … valueXN; "timeX1", "timeX2", … "timeXN"]  

Array_nameY = [ "valueY1", "valueY2" … valueYN; "timeY1", "timeY2", … "timeYN"] 

Array_nameZ = [ "valueZ1", "valueZ2" … valueZN; "timeZ1", "timeZ2", … "timeZN"] 
and finally construct an dictionnary with the key value the name of my signal, and the value as the different arrays

 dict = [X: array_nameX, Y:array_nameY,...]
Reply
#2
A defaultdict can solve the task.

from collections import defaultdict


data = [{"signal_name":"X", "signal_value":"valueX1", "time":"timeX1","par":"id1"}, 
{"signal_name":"Y", "signal_value":"valueY1", "time":"timeY1","par":"id2"},
{"signal_name":"Z", "signal_value":"valueZ1", "time":"timeZ1","par":"id3"},
{"signal_name":"X", "signal_value":"valueX2", "time":"timeX2","par":"id5"}]

measurements = defaultdict(list)
for signal in data:
    name, value = signal['signal_name'], signal['signal_value']
    measurements[name].append(value)
PS: The original data in your post has a syntax error. You're using a = two times instead of a colon.
It's also not json, it's a Python dict. Json was it before in form of a string.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parse json field from csv file lebossejames 4 668 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,344 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,960 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,165 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,221 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,527 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,337 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Python Split json into separate json based on node value CzarR 1 5,472 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Initializing, reading and updating a large JSON file medatib531 0 1,723 Mar-10-2022, 07:58 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

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