2

I'm trying to convert normal json string to new line delimited json format (ndjson). I found the below unix command to perform the same.

cat test.json | jq -c '.[]' > testNDJSON.json

I need an equivalent java functionality.

PFB the json string which I want to convert to ndjson.

[{"id":1266052,"date1":"Jun 2, 2020 12:00:00 AM","userId":721187,"externalSource":"B","completedCardioActivityDataVOList":[],"completedStrengthActivityVO":{"activityId":110303,"lfCodeId":1,"intensity":2,"duration":180,"recommendedValueArray":"4","lfCalories":18.0,"createdAt":"Sep 25, 2017 1:25:16 PM","updatedAt":"Sep 25, 2017 1:25:16 PM"},"completedStrengthActivityDataVOList":[{"id":630060,"setNumber":1,"actualRepetitions":30,"actualResistanceEn":60.0,"actualResistanceMt":27.215542}]},{"id":1339904,"date1":"Jun 2, 2020 12:00:00 AM","userId":721187,"externalSource":"B","guid":"17b86829-0b38-4a95-a3a3-6dc006b3a979","completedCardioActivityVO":{"activityId":1,"workoutId":1},"completedCardioActivityDataVOList":[{"id":8058043,"cardioParameterId":7,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":25.6,"actualCardioParameterValueEn":25.6},{"id":8058044,"cardioParameterId":5,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":139.0,"actualCardioParameterValueEn":139.0},{"id":8058045,"cardioParameterId":13,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":88.0,"actualCardioParameterValueEn":88.0},{"id":8058046,"cardioParameterId":12,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":0.0,"actualCardioParameterValueEn":0.0},{"id":8058047,"cardioParameterId":25,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":0.0,"actualCardioParameterValueEn":0.0},{"id":8058048,"cardioParameterId":24,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":0.0,"actualCardioParameterValueEn":0.0},{"id":8058049,"cardioParameterId":6,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":27.358849,"actualCardioParameterValueEn":17.0},{"id":8058050,"cardioParameterId":14,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":5.365553,"actualCardioParameterValueEn":3.334},{"id":8058051,"cardioParameterId":4,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":2.2880044,"actualCardioParameterValueEn":1.4217},{"id":8058052,"cardioParameterId":21,"targetCardioParameterValueMt":0.0,"targetCardioParameterValueEn":0.0,"actualCardioParameterValueMt":30.7848,"actualCardioParameterValueEn":101.0}],"completedStrengthActivityDataVOList":[]}]

Jefila
  • 131
  • 1
  • 11
  • what JSON parser are you using? the equivalent is to parse the JSON, iterate over the array elements, and output stringify each array element with a newline appended to it. write your Java code for parsing the JSON first and put it here – user120242 Jun 02 '20 at 08:16
  • I've added the json string which I want to convert to ndjson. I'm using Gson. – Jefila Jun 02 '20 at 09:09
  • 1
    Have you tried these two libraries: https://github.com/jignaasu/ndjson-java & https://github.com/abilng/ndjson? – dk7 Jun 02 '20 at 09:14
  • Thanks @dk7. The second link helped me. But when handling large data, the json parsing is taking long time. Is there a way to reduce the time taken? Instead of converting the List to JsonArray and then List. Then doing the stringify? – Jefila Jun 03 '20 at 07:07
  • You're welcome @Jefila! I'm sorry but I didn't have any use case like that, so I'm out of ideas for now :( – dk7 Jun 04 '20 at 09:21

0 Answers0