Jackson example – Read and write JSON

In this post,we will see how can we read and write JSON using Jackson.

Java JSON Tutorial Content:

After  reading and writing JSON using GSON,we will use another way(i.e. Jackson) of reading JSON.
Jackson is a high performance Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

writeValue : It will convert simple pojo object to JSON.

readValue : It will convert JSON to pojo object.

Please note that there are many overloaded version of read and write value to support different parameters such as File,URL

In this post,we will read and write JSON using Jackson.
1). Create a java project named “jacksonJsonExample”

2) Add following maven dependency for jackson.
Pom.xml :

3) create a pojo named “Country.java”

Write JSON to file:

4) Create a new class named “JacksonJsonWriteToFileExample.java” in src->org.arpit.java2blog.jacksonJsonExample

Run above program and content of file country.json will be as below :

Write formatted JSON to file:

5) If you want to prettify json, you an use mapper.writerWithDefaultPrettyPrinter() to do it. Create a new class named “JacksonJsonWriteToFileFormattedExample.java” in src->org.arpit.java2blog.jacksonJsonExample

Run above program and content of file country.json will be as below :

Read JSON to file:

Here we will read above created JSON file.
6) Create a new class named “JacksonJsonReadFromFileExample.java” in src->org.arpit.java2blog.jacksonJsonExample

Run above program and you will get following output:
click to begin
20KB .zip

Project Structure:

Related Posts

  • 11 August

    Create A REST API With JSON Server

    In this tutorial, we will see about JSON server. Let’s say you are front-end developer and need to integrate backend calls with REST APIs but you came to know from backend team that REST APIs are not completed yet and will take some time to do the coding. How will you proceed now? JSON server […]

  • 25 September

    Gson fromJson example

    Table of ContentsRead simple JSON StringRead JSON Array to ListRead Json String and convert it to Map In this post, we will see about Gson fromJson example.We have already seen simple Gson example in the previous post. Gson fromJson method is used to convert JSON String or JsonObject or Reader to the corresponding object. You […]

  • 04 October

    Jackson Streaming API – read and write json

    Table of ContentsJava JSON Tutorial Content:Write JSON to file:Read JSON to file: In this post,we will see how can we will use jackson streaming API to read and write JSON . Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSONJackson example – read and write JSON Jackson Streaming […]

  • 17 November

    GSON example – Read and write JSON

    In this post,we will see how can we read and write JSON using GSON. Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSON Jackson example – read and write JSON Jackson Streaming API – read and write JSON reading and writing JSON using json-simple.We will use another way(i.e. […]

  • 17 November

    JSON.simple example – Read and write JSON

    In this post,we will see how can we read and write JSON using json.simple. Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSON Jackson example – read and write JSON Jackson Streaming API – read and write JSON JSON.simple, is a simple Java library for JSON processing, read […]

  • 17 November

    Java JSON tutorial

    In this post,We will see introduction to JSON Java JSON Tutorial Content: JSON Introduction JSON.simple example-read and write JSON GSON example-read and write JSON Jackson example – read and write JSON Jackson Streaming API – read and write JSON What is JSON? JSON stands for JavaScript Object Notation.It  is a simple and easy to read […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.