Kamal Pradhan Kamal Pradhan - 2 months ago 6
Java Question

How to converting a nested Json object/array to multiple lists based on keys Dynamically without knowing the keys


The below is a sample Json file.


{"Yjson":
[
{
"Name": "crunchify.com",
"Author": "App Shah",
"Address": "New York",
"Company Services": [{
"Service": "Site SEO Review",
"Link": "https://crunchify.com/services/site-seo-review-service/"
}, {
"Service": "Full Website Design Service",
"Link": "https://crunchify.com/services/full-website-design-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}]
},
{
"Name": "xyz.com",
"Author": "xyz Shah",
"Address": "toronto",
"Company Services": [{
"Service": "Site SEO Review",
"Link": "https://crunchify.com/services/site-seo-review-service/"
}, {
"Service": "Full Website Design Service",
"Link": "https://crunchify.com/services/full-website-design-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}]
}
]
}



How to store all the values of each key in a Arraylist in java ?


such as the array list for key name will contain [crunchicy.com,xyz.com]. similarly for every key there should be a array list.

example :


  • list name : [crunchify.com,xyz]

  • list Author : [aap shah,xyz shah]

  • list name : [newyork,toronto]




How to parse every json object that can be nested and dynamic in
nature and keep them in array lists according to keys ??

Answer

Here is code for reading your JSON text using Jackson Databind and writing it to CSV using Apache Commons CSV.

When using Databind, you need Java POJO classes, optionally annotated using @JsonProperty to specify the JSON field names.

Your JSON text is included inline at the bottom to produce an MCVE.

import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {
    public static void main(String[] args) throws Exception {
        Root root = new ObjectMapper().readValue(Input.json, Root.class);
        CSVPrinter printer = CSVFormat.DEFAULT
                                      .withHeader("Name", "Author", "Address", "Service", "Link")
                                      .print(System.out);
        for (Site site : root.getSites())
            for (Service service : site.getServices())
                printer.printRecord(site.getName(), site.getAuthor(), site.getAddress(),
                                    service.getService(), service.getLink());
    }
}
class Root {
    private List<Site> sites;

    @JsonProperty("Yjson")
    public List<Site> getSites() {
        return this.sites;
    }
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }
}
class Site {
    private String        name;
    private String        author;
    private String        address;
    private List<Service> services;

    @JsonProperty("Name")
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("Author")
    public String getAuthor() {
        return this.author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    @JsonProperty("Address")
    public String getAddress() {
        return this.address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    @JsonProperty("Company Services")
    public List<Service> getServices() {
        return this.services;
    }
    public void setServices(List<Service> services) {
        this.services = services;
    }
}
class Service {
    private String service;
    private String link;

    @JsonProperty("Service")
    public String getService() {
        return this.service;
    }
    public void setService(String service) {
        this.service = service;
    }

    @JsonProperty("Link")
    public String getLink() {
        return this.link;
    }
    public void setLink(String link) {
        this.link = link;
    }
}
class Input {
    static final String json =
        "{\"Yjson\":\n" +
        "[\n" +
        "{\n" +
        "\"Name\": \"crunchify.com\",\n" +
        "    \"Author\": \"App Shah\",\n" +
        "    \"Address\": \"New York\",\n" +
        "    \"Company Services\": [{\n" +
        "        \"Service\": \"Site SEO Review\",\n" +
        "        \"Link\": \"https://crunchify.com/services/site-seo-review-service/\"\n" +
        "    }, {\n" +
        "        \"Service\": \"Full Website Design Service\",\n" +
        "        \"Link\": \"https://crunchify.com/services/full-website-design-service/\"\n" +
        "    }, {\n" +
        "        \"Service\": \"WordPress Optimization & Consultation\",\n" +
        "        \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
        "    }, {\n" +
        "        \"Service\": \"WordPress Optimization & Consultation\",\n" +
        "        \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
        "    }]\n" +
        "},\n" +
        "{\n" +
        "    \"Name\": \"xyz.com\",\n" +
        "    \"Author\": \"xyz Shah\",\n" +
        "    \"Address\": \"toronto\",\n" +
        "    \"Company Services\": [{\n" +
        "        \"Service\": \"Site SEO Review\",\n" +
        "        \"Link\": \"https://crunchify.com/services/site-seo-review-service/\"\n" +
        "    }, {\n" +
        "        \"Service\": \"Full Website Design Service\",\n" +
        "        \"Link\": \"https://crunchify.com/services/full-website-design-service/\"\n" +
        "    }, {\n" +
        "        \"Service\": \"WordPress Optimization & Consultation\",\n" +
        "        \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
        "    }]\n" +
        "}\n" +
        "]\n" +
        "}";
}

Output

Name,Author,Address,Service,Link
crunchify.com,App Shah,New York,Site SEO Review,https://crunchify.com/services/site-seo-review-service/
crunchify.com,App Shah,New York,Full Website Design Service,https://crunchify.com/services/full-website-design-service/
crunchify.com,App Shah,New York,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
crunchify.com,App Shah,New York,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
xyz.com,xyz Shah,toronto,Site SEO Review,https://crunchify.com/services/site-seo-review-service/
xyz.com,xyz Shah,toronto,Full Website Design Service,https://crunchify.com/services/full-website-design-service/
xyz.com,xyz Shah,toronto,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/

If you use Maven, these are the two dependencies you need:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.4</version>
</dependency>