How to convert pretty format json file into a simple single line json file using java?
Asked Answered
S

5

6

I need to conver Pretty format json file into a single line json file. But unable to do so. Can anyone help me on this?

Example: the below needs to be converted

[
 {
  "Employee ID": 1,
  "Name": "Abhishek",
  "Designation": "Software Engineer"
 },
 {
  "Employee ID": 2,
  "Name": "Garima",
  "Designation": "Email Marketing Specialist"
 }
]
[
   {
      "Employee ID": 1,
      "Name": "Abhishek",
      "Designation": "Software Engineer"
   },
   {
      "Employee ID": 2,
      "Name": "Garima",
      "Designation": "Email Marketing Specialist"
   }
]

to this:

 '[{"Employee ID":1,"Name":"Abhishek","Designation":"Software Engineer"},' \ 
            '{"Employee ID":2,"Name":"Garima","Designation":"Email Marketing Specialist"}]'
Samualsamuel answered 17/2, 2021 at 8:46 Comment(2)
If you really need to get the result file, but not the entire text in the memory, I wouldn't use the receipts below, but just read the original file line by line, make a trim and write the result string to the output file: try (BufferedWriter/FileWiter output = ...; Stream<String> stream = Files.lines(Paths.get(fileName))) { stream.forEach( /* trim and write the result string into output}}. Store only what is really required to make your job, save your resources :)Briquet
Makes sense, Thankyou! :)Samualsamuel
I
5

A better and more complete version of this answer with proper error handling and removal of extra spaces.

One problem of the referred answer is that it uses .concat() without .trim() - a very deeply nested pretty-JSON's indents will be visible as extra spaces in final output.

String unprettyJSON = null;

try {
    unprettyJSON = Files.readAllLines(Paths.get("pretty.json"))
                        .stream()
                        .map(String::trim)
                        .reduce(String::concat)
                        .orElseThrow(FileNotFoundException::new);
} catch (IOException e) {
    e.printStackTrace();
}

Output with trim:

[{"Employee ID": 1,"Name": "Abhishek","Designation": "Software Engineer"},{"Employee ID": 2,"Name": "Garima","Designation": "Email Marketing Specialist"}][{"Employee ID": 1,"Name": "Abhishek","Designation": "Software Engineer"},{"Employee ID": 2,"Name": "Garima","Designation": "Email Marketing Specialist"}]

Output without trim:

[ {  "Employee ID": 1,  "Name": "Abhishek",  "Designation": "Software Engineer" }, {  "Employee ID": 2,  "Name": "Garima",  "Designation": "Email Marketing Specialist" }][   {      "Employee ID": 1,      "Name": "Abhishek",      "Designation": "Software Engineer"   },   {      "Employee ID": 2,      "Name": "Garima",      "Designation": "Email Marketing Specialist"   }]
Import answered 17/2, 2021 at 9:38 Comment(2)
Domenico Sibilio and Harshal Parekh it worked as expected, thanks!!Samualsamuel
Hi @Harshal Parekh , it is working as expected but my json is very large, any way to make process fat, it is taking some time for meConsultation
I
2

If you read the file contents via Files.readAllLines(Path) then you already get each line as a different item in a list, which you can in turn merge together into a single line.

For example:

String oneLine = Files.readAllLines(Paths.get("input.json"))
.stream()
.reduce(String::concat)
.orElseThrow();

System.out.println(oneLine);
Isabelleisac answered 17/2, 2021 at 8:56 Comment(0)
T
0
try {
    unprettyJSON = Files.readAllLines(Paths.get("pretty.json"))
                        .stream()
                        .map(String::trim)
                        .reduce(String::concat)
                        .orElseThrow(FileNotFoundException::new);
} catch (IOException e) {
    e.printStackTrace();
}

This above solution works nicely. Thanks for posting it. You can read from a .json file which converts to a single line string which can be also used to POST https requests.

Tease answered 28/12, 2021 at 0:3 Comment(0)
W
0

im using sed for this

sed ':a;N;$!ba;s/\n//g' demo.json

this would basically change each new line to be the same line. hope it helped :)

Warbeck answered 5/3, 2023 at 16:33 Comment(0)
N
0
String json = cn.hutool.core.io.FileUtil.FileUtil.readUtf8String(file);
System.out.println(com.alibaba.fastjson.JSON.parseArray(json).toJSONString());
Ninette answered 1/6 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.