Posted In: JSON

Convert json string to pretty print json output using JSON library

    1. Example will use following Test data   2. Using GSON.toJson   3. Using Jackson 2X writerWithDefaultPrettyPrinter   4. Using Jackson 1X writerWithDefaultPrettyPrinter   5. Using org.json.JSONObject.toString(indent) from org.json library   6. Using net.sf.json.JSONObject.toString(indent) from net.sf.json-lib library   7. Output { “portfolio” : [ { “stock” : “Infosys”, “quantity” : “100”, “buy” : […]

Read More

Tags: Tags: , , , , ,

by , on February 26th, 2017

Posted In: JSON

Check two JSON Objects for equality – Jackson

  This example uses jackson library     Examples using Hamcrest-JSON   1. Success test with a JSON string   2. Error test with a JSON string   3. Success test with a JSON String This test covers How do I compare 2 json files having same key-values but in different order in java?   […]

Read More

Tags: Tags: , ,

by , on February 24th, 2017

Posted In: JSON

Are escaping special characters in JSON string required?

  Following tests with various libraries confirms that JSON library will handle special characters and there is no need to use StringEscapeUtilities.escapeHtml, StringEscapeUtilities.escapeXml or java.net.URLEncoder   1. Example will use following Test data   2. Using GSON   3. Using Jackson 2X   4. Using Jackson 1X   5. Using com.jsoniter.output.JsonStream from json iterator library […]

Read More

Tags: Tags: , , , , ,

by , on February 22nd, 2017

Posted In: JSON

How to parse JSON to POJO in Java

  This example uses general JSONObject or Any object provided by library. To parse JSON to your own application POJO refere this.   1. Example will use following Test data   2. Using GSON   3. Using Jackson 2X   4. Using Jackson 1X   5. Using JsonIterator from json iterator library   6. Using […]

Read More

Tags: Tags: , , , , ,

by , on February 22nd, 2017

Posted In: JSON

How to parse JSON in Java – Without POJO

  This example uses general JSONObject or Any object provided by library. To parse JSON to your own application POJO refer how-to-parse-json-to-pojo-in-java   1. Example will use following Test data   2. Using GSON   3. Using Jackson 2X   4. Using Jackson 1X   5. Using JsonIterator from json iterator library   6. Using […]

Read More

Tags: Tags: , , , , ,

by , on February 22nd, 2017

Posted In: JSON

How to convert ArrayList to JSON object in Java

  1. Example will use following Test data   2. Using GSON   3. Using Jackson 2X   4. Using Jackson 1X   5. Using com.jsoniter.output.JsonStream from json iterator library   6. Using net.sf.json.JSONArrayfrom net.sf.json-lib library   7. Using javax.json.JsonArray, javax.json.JsonArrayBuilder from javax.json library   8. Using org.json.JSONArray from org.json library   9. Output [“val1″,”val2″,”val3″,”val4″,”val5”] […]

Read More

Tags: Tags: , , , , , , , , ,

by , on February 22nd, 2017

Posted In: JSON

How to convert hashmap to JSON object in Java

  1. Example will use following Test data   2. Using GSON   3. Using Jackson 2X   4. Using Jackson 1X   5. Using com.jsoniter.output.JsonStream from json iterator library   6. Using net.sf.json.JSONObject from net.sf.json-lib library   7. Using javax.json.JsonObject, javax.json.JsonObjectBuilder from javax.json library   8. Using org.json.JSONObject from org.json library   9. Output […]

Read More

Tags: Tags: , , , , , , , , ,

by , on February 21st, 2017

Posted In: Jackson 2X, JSON

Generate Json Schema from POJO – Jackson 2X

  1. Example is using Address POJO with multiple fields.   2. Use com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator class to get schema from POJO class       3. Schema generated    

Read More

Tags: Tags: ,

by , on February 21st, 2017

Posted In: Jackson 2X, JSON

Generate Java POJO from JSON or JSON schema – Annotation style – Jackson 2.x

  We created POJOs from JSON in previous example generate-java-pojo-from-json-or-json-schema. Let us go through some options that we have on http://www.jsonschema2pojo.org/   Annotation style – Jackson 2.x This will add annotations like @JsonProperty, @JsonPropertyOrder, @JsonInclude to your POJOs. Jackson will use these annotations for mapping fields to JSON. It has side effect and cause production […]

Read More

Tags: Tags: , , , , , ,

by , on February 20th, 2017