Home > Software engineering >  Using GSON to update a JSON file
Using GSON to update a JSON file

Time:03-24

Hi I'm new to GSON and I am currently learning how to use it. I'm using a JSON file that looks like this:

{"2b3e6902":["4CA84C",46.89,-7.11,16.0,38000.0,440.0,"7460","F-EGHD3","B738","EI-EMD",1.648076652E9,"TFS","EDI","FR6622",0.0,0.0,"RYR66M",0.0,"RYR"],"2b3eccf4":["400513",54.65,-6.23,243.0,375.0,150.0,"1406","F-EGAA2","B734","G-POWS",1.648076654E9,"BFS","STN","",0.0,2816.0,"AWC2B",0.0,"AWC"],"2b3eba93":["404529",54.66,-6.21,191.0,0.0,1.0,"7000","F-EGAA2","P28A","G-ISAX",1.648076655E9,"BFS","","",1.0,0.0,"GISAX",0.0,""],"2b3eaefc":["AA9BB7",50.06,-6.02,287.0,30975.0,470.0,"5644","F-EGHE3","B788","N783AM",1.648076655E9,"CDG","MEX","AM4",0.0,0.0,"AMX004",0.0,"AMX"],"2b3e4c95":["4CAFB5",53.39,-5.94,352.0,3425.0,206.0,"4475","F-EGNS1","B738","EI-GSI",1.648076654E9,"ACE","DUB","FR7125",0.0,-64.0,"RYR4SK",0.0,"RYR"],"2b3e5eb7":["4CA80B",54.21,-5.69,24.0,38000.0,418.0,"7451","F-EGBB3","B738","EI-EKK",1.648076654E9,"RAK","EDI","RK515",0.0,0.0,"RUK515",0.0,"RYR"],"2b3e5abe":["407689",49.51,-5.56,23.0,38025.0,438.0,"7456","F-EGFF2","A20N","G-UZHU",1.648076654E9,"LPA","MAN","U21910",0.0,0.0,"EZY45QH",0.0,"EZY"],"2b3ec826":["40761A",54.17,-5.27,128.0,23250.0,430.0,"1404","F-EGXU1","B734","G-NPTX",1.648076655E9,"BFS","EMA","",0.0,1856.0,"NPT01C",0.0,"NPT"],"2b3e4af8":["4CAA58",51.5,-5.21,44.0,37000.0,427.0,"6205","F-EGNR5","B738","EI-EVS",1.648076653E9,"TFS","MAN","FR4331",0.0,64.0,"RYR56SC",0.0,"RYR"],"2b3eb7d4":["406F77",50.18,-5.19,249.0,31725.0,495.0,"7651","F-EGHE3","B789","G-ZBKK",1.648076655E9,"LHR","SCL","BA251",0.0,896.0,"BAW251",0.0,"BAW"],"2b3ea116":["E494F9",44.47,-4.98,214.0,35000.0,469.0,"7102","F-LEXJ2","A339","PR-ANW",1.648076653E9,"BRU","GRU","AD9998",0.0,0.0,"AZU9998",0.0,"AZU"],"2b3e5fc3":["4CA891",53.5,-4.81,262.0,14100.0,374.0,"3121","F-EGCC1","B738","EI-ENE",1.648076653E9,"OTP","DUB","FR7347",0.0,-2944.0,"RYR3UU",0.0,"RYR"],"2b3e60c9":["4CA8E7",49.6,-3.82,17.0,38000.0,422.0,"7455","F-LFRQ5","B738","EI-ENV",1.648076653E9,"ACE","BHX","FR1449",0.0,0.0,"RYR4CF",0.0,"RYR"],"2b3ec614":["4CA7B4",53.49,-3.73,291.0,28000.0,356.0,"1401","F-EGTR2","B738","EI-EFY",1.648076655E9,"MAN","DUB","FR559",0.0,-64.0,"RYR559",0.0,"RYR"]

My goal is to create a new JSON file that would look like this:

{"2b3e6902":
   ["flight_id":"4CA84C",
   "latitude":46.89,
   "longitude":-7.11,
   "track":16.0,
   "altitude":38000.0,
   "speed":440.0,
   "squawk":"7460",
   "radarID":"F-EGHD3",
   "aircraftType":"B738",
   "registration":"EI-EMD",
   "timestamp":1.648076652E9,
   "from":"TFS",
   "to":"EDI",
   "altID":"FR6622",
   "Boolean1":0.0,
   "Boolean2":0.0,
   "callSign":"RYR66M",
   "Boolean3":0.0,
   "IATA":"RYR"]}

Is there any way I can accomplish that using GSON? Thank you in advance

CodePudding user response:

Seems to me you have a JSON where a key is a string and the value is a list of different items. You should create a POJO according to your desired JSON and then while looping through every key of your current JSON, create an object of your POJO where you pass values from the list to the constructor.

CodePudding user response:

the initial string is a valid JSON string, since the value is an array of elements.

{"2b3e6902":["4CA84C",46.89,-7.11,16.0,38000.0,440.0,"7460","F-EGHD3","B738","EI-EMD",1.648076652E9,"TFS","EDI","FR6622",0.0,0.0,"RYR66M",0.0,"RYR"],"2b3eccf4":["400513",54.65,-6.23,243.0,375.0,150.0,"1406","F-EGAA2","B734","G-POWS",1.648076654E9,"BFS","STN","",0.0,2816.0,"AWC2B",0.0,"AWC"],"2b3eba93":["404529",54.66,-6.21,191.0,0.0,1.0,"7000","F-EGAA2","P28A","G-ISAX",1.648076655E9,"BFS","","",1.0,0.0,"GISAX",0.0,""],"2b3eaefc":["AA9BB7",50.06,-6.02,287.0,30975.0,470.0,"5644","F-EGHE3","B788","N783AM",1.648076655E9,"CDG","MEX","AM4",0.0,0.0,"AMX004",0.0,"AMX"],"2b3e4c95":["4CAFB5",53.39,-5.94,352.0,3425.0,206.0,"4475","F-EGNS1","B738","EI-GSI",1.648076654E9,"ACE","DUB","FR7125",0.0,-64.0,"RYR4SK",0.0,"RYR"],"2b3e5eb7":["4CA80B",54.21,-5.69,24.0,38000.0,418.0,"7451","F-EGBB3","B738","EI-EKK",1.648076654E9,"RAK","EDI","RK515",0.0,0.0,"RUK515",0.0,"RYR"],"2b3e5abe":["407689",49.51,-5.56,23.0,38025.0,438.0,"7456","F-EGFF2","A20N","G-UZHU",1.648076654E9,"LPA","MAN","U21910",0.0,0.0,"EZY45QH",0.0,"EZY"],"2b3ec826":["40761A",54.17,-5.27,128.0,23250.0,430.0,"1404","F-EGXU1","B734","G-NPTX",1.648076655E9,"BFS","EMA","",0.0,1856.0,"NPT01C",0.0,"NPT"],"2b3e4af8":["4CAA58",51.5,-5.21,44.0,37000.0,427.0,"6205","F-EGNR5","B738","EI-EVS",1.648076653E9,"TFS","MAN","FR4331",0.0,64.0,"RYR56SC",0.0,"RYR"],"2b3eb7d4":["406F77",50.18,-5.19,249.0,31725.0,495.0,"7651","F-EGHE3","B789","G-ZBKK",1.648076655E9,"LHR","SCL","BA251",0.0,896.0,"BAW251",0.0,"BAW"],"2b3ea116":["E494F9",44.47,-4.98,214.0,35000.0,469.0,"7102","F-LEXJ2","A339","PR-ANW",1.648076653E9,"BRU","GRU","AD9998",0.0,0.0,"AZU9998",0.0,"AZU"],"2b3e5fc3":["4CA891",53.5,-4.81,262.0,14100.0,374.0,"3121","F-EGCC1","B738","EI-ENE",1.648076653E9,"OTP","DUB","FR7347",0.0,-2944.0,"RYR3UU",0.0,"RYR"],"2b3e60c9":["4CA8E7",49.6,-3.82,17.0,38000.0,422.0,"7455","F-LFRQ5","B738","EI-ENV",1.648076653E9,"ACE","BHX","FR1449",0.0,0.0,"RYR4CF",0.0,"RYR"],"2b3ec614":["4CA7B4",53.49,-3.73,291.0,28000.0,356.0,"1401","F-EGTR2","B738","EI-EFY",1.648076655E9,"MAN","DUB","FR559",0.0,-64.0,"RYR559",0.0,"RYR"]}

you need to loop through that array in order to convert it to the object you want, the GSON libraries are only going to give you the same result, additionally the JSON string that you put in the question is in an invalid format, since you must use { instead of [, like this:

{"2b3e6902":
   {"flight_id":"4CA84C",
   "latitude":46.89,
   "longitude":-7.11,
   "track":16.0,
   "altitude":38000.0,
   "speed":440.0,
   "squawk":"7460",
   "radarID":"F-EGHD3",
   "aircraftType":"B738",
   "registration":"EI-EMD",
   "timestamp":1.648076652E9,
   "from":"TFS",
   "to":"EDI",
   "altID":"FR6622",
   "Boolean1":0.0,
   "Boolean2":0.0,
   "callSign":"RYR66M",
   "Boolean3":0.0,
   "IATA":"RYR"}
  
}

In other words, i think that you can't convert an array of elements to an object with just the GSON libraries.

I hope my answer has been helpful.

  • Related