TransWikia.com

arranging the json file base on key value

Stack Overflow Asked by Prateek Singh on December 5, 2021

I have json data in a file in the below format. I have 1000’s of pkt .(2of them shown below)

{
"SensorData": 
{ "LeadStatus": 0, 
"Respiration": [1647240, 1647038, 1646865, 1646807], 
"PatchId": "TUVT1", 
"TsSpO2": 6262999470, 
"SpO2": [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 
"Accel": [7, 0, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -932, 15201, 5515, -930, 15194, 5514, -938, 15189, 5516, -934, 15195, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
"IAGain": 0, 
"vBat": 2749, 
"Seq": 25053, 
"TsECG": 6263250944, 
"ACCEL": [-32768, -32768, -32768, -32768, -32768, -32768, -32768, -932, 15201, 5515, -930, 15194, 5514, -938, 15189, 5516, -934, 15195, 0, 0, 0], "ECG1": [-41, -47, -11, 27, 64, 103, 141, 183, 189, 145, 105, 63, 22, -19, -56, -47, -27, -8, -2, -4, -3, -3, -4, -3, -3, -3, -3, -2, -3, -2, -3, -3, -2, -3, -2, -2, -1, -1, -1, 0, 1, 0, 2, 2, 3, 3, 3, 5, 6, 7, 8, 8, 10, 10, 11, 14, 16, 17, 18, 19, 21], "ECG0": [-15, -15, 6, 31, 56, 80, 102, 129, 134, 107, 84, 57, 32, 6, -16, -14, -6, -2, -1, 0, -1, -1, -1, -1, 0, -1, 0, 0, -1, 0, 0, -1, -1, 0, 1, 1, 1, 2, 1, 2, 3, 3, 3, 4, 5, 4, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11, 12, 12, 14, 15, 17], "RLDInformation": 0
}
}

#2nd pkt comes in next line.

{
"SensorData": 
{"LeadStatus": 0, 
"Respiration": [1646840, 1646829, 1646680, 1646472], 
"PatchId": "TUVT1", 
"TsSpO2": 6263249460, 
"SpO2": [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 
"Accel": [7, 0, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -936, 15201, 5521, -937, 15202, 5520, -937, 15202, 5520, -931, 15197, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
"IAGain": 0, 
"vBat": 2749, 
"Seq": 25054, 
"TsECG": 6263500800, 
"ACCEL": [-32768, -32768, -32768, -32768, -32768, -32768, -32768, -936, 15201, 5521, -937, 15202, 5520, -937, 15202, 5520, -931, 15197, 0, 0, 0], "ECG1": [22, 24, 27, 30, 34, 36, 38, 42, 44, 42, 41, 38, 37, 37, 36, 36, 34, 27, 20, 12, 7, 4, 1, -2, -4, -3, -3, -4, -2, -4, -3, -3, -3, -3, -3, -3, -4, -3, -3, -3, -2, -3, -4, -3, -2, -3, -3, -3, -3, -2, -3, -3, -3, -2, -3, -3, -3, -3, -3, -3, -3], 
"ECG0": [17, 18, 20, 24, 25, 28, 29, 30, 32, 30, 30, 29, 28, 27, 27, 28, 24, 20, 17, 10, 7, 4, 4, 1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, -1, 0, 0, -2, 0, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, -1, 0, 0, -1, -1], 
"RLDInformation": 0, 
"Temperature": -32768}}

I want to sort this based on value of ‘Seq’ key.and write it in a new file. I used the code below.

    import json
    data=[]
    fp = open('RecData_1.json','r')
    for line in fp:
      data.append(json.loads(line))
    fp.close()

    def rev_sorting(json):
      try:
        return (json['SensorData']['Seq'])
      except KeyError:
        return 0
    data.sort(key=rev_sorting, reverse=True)

    with open("sample.json","w") as outfile:
      for line in data:
        json.dump(line,outfile)
    outfile.close()

when i run my code it does create a new file, but it doesn’t open.

I took help from below responses.
How do I write JSON data to a file?
python sort list of json by value

One Answer

You just have to dump the list of sorted jsons and when using context managers you don't need to close the file object

with open("sample.json","w") as outfile:
      json.dump(data, outfile)

Or if you want the jsons to be in newline

with open("sample.json","w") as outfile:
      for line in data:
          outfile.write(json.dumps(line) + "n")

Answered by bigbounty on December 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP