0

I have a JSON file that I downloaded and I don't know its nodes but I want to beautify it using java.

I found many solutions but in my case, I don't know the nodes of my JSON file. So any help please

How I want it to be

How it is

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
maryem neyli
  • 467
  • 3
  • 20
  • Refer: https://stackoverflow.com/questions/5457524/json-beautifier-library-for-java – Ashish Karn Feb 27 '20 at 11:45
  • What are those solutions you tried and didn't work? – Federico klez Culloca Feb 27 '20 at 11:45
  • Maybe: https://stackoverflow.com/questions/14515994/convert-json-string-to-pretty-print-json-output-using-jackson – pandaadb Feb 27 '20 at 11:45
  • 1
    Does this answer your question? [JSON Beautifier Library for Java](https://stackoverflow.com/questions/5457524/json-beautifier-library-for-java) – spyr03 Feb 27 '20 at 12:05
  • @FedericoklezCulloca I swear I tried all the solutions mentioned above but the problem that in order to build a JSON well-formed file using java(Jackson, Gson, json ...) you need to know the nodes of your file but I don't because, in my case, this JSON file well be dynamic: every download brings me a new file so it needs to be beautified every time – maryem neyli Feb 27 '20 at 13:10
  • @maryemneyli uhm... no you don't? If you read the very first answer to the very first link you've been given you'll notice that it doesn't mention the json's fields anywhere. – Federico klez Culloca Feb 27 '20 at 13:13

1 Answers1

3

I swear I tried all the solutions mentioned above but the problem that in order to build a JSON well-formed file using java (Jackson, Gson, json ...) you need to know the nodes of your file but I don't [...]

If you are open to use Jackson, here's a solution that will work:

ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT);

JsonNode jsonNode = mapper.readTree(new URL("https://petstore.swagger.io/v2/swagger.json"));
String formattedJson = mapper.writeValueAsString(jsonNode);

The example above demonstrates how to read a JSON document from a given URL, which may or may not be your case. If it doesn't suit your needs, check the ObjectMapper API, as you can read JSON documents from a String, from an InputStream, from a File and others.

The main thing here is: This solution doesn't require you to know the structure of the JSON. As long it's a valid JSON, Jackson will use its tree model to parse it.


Alternatively, as you seem to want to parse a Swagger/OpenAPI 2.0 JSON file, you can use the following (check the documentation for details):

Swagger swagger = new SwaggerParser().read("https://petstore.swagger.io/v2/swagger.json");
String swaggerString = Json.pretty(swagger);
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Isn't this identical to the combination of the first two answers in the [twice linked duplicate](https://stackoverflow.com/questions/5457524/json-beautifier-library-for-java)? – Federico klez Culloca Feb 27 '20 at 14:25
  • @FedericoklezCulloca I've just checked the links you referred. I agree that one of the solutions I provided is quite similar to one of the existing answers. But you also have to agree that none of those answers mention the usage of `SwaggerParser`. So I don't entirely think this answer deserves a downvote. – cassiomolin Feb 27 '20 at 14:30
  • @cassiomolin thank u ! I'm sooo grateful ! u actually saved my career – maryem neyli Feb 27 '20 at 14:39
  • 1
    @cassiomolin yeah, I reverted that downvote for that reason, even before you commented as I gave it a bit more thought :) – Federico klez Culloca Feb 27 '20 at 14:49