-1

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 ??

PradhanKamal
  • 540
  • 4
  • 18
  • Did you try anything? – Oliver Charlesworth Sep 11 '16 at 07:49
  • Do not store each field in separate lists. Java is an Object-Oriented language, and you should use it. Create a class, e.g. `Site`, and have only one list, e.g. `List`. Jackson can serialize directly to that for you. – Andreas Sep 11 '16 at 07:51
  • see http://stackoverflow.com/questions/35973938/how-to-parse-a-3d-json-array-in-java/35974076#35974076 – M Sach Sep 11 '16 at 08:25
  • @OliverCharlesworth yes i tried to flatten the json string into map by using a library json-flattener but it doesn't as per my requirement. what i want is to parse the json so that it can be converted into tabular format such as csv. – PradhanKamal Sep 11 '16 at 08:58
  • @Andreas i'll definitely try that but what i want is to parse the json so that it can be converted into tabular format such as csv. – PradhanKamal Sep 11 '16 at 08:59
  • @MSach i'll try but what i want is to parse the json so that it can be converted into tabular format such as csv. – PradhanKamal Sep 11 '16 at 09:00
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Geralt_Encore Sep 11 '16 at 09:38
  • @Geralt_Encore in the post you mentioned above we have to specify the key for parsing the values. what to do if we have to do it dynamically without knowing the key. – PradhanKamal Sep 11 '16 at 09:51

1 Answers1

1

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>
Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • i agree with the results. but what i need is a generalized approach that will work even if the keys are changed. it should work if we use a different json file with different keys all together. in the above code the keys are specified in the code. can we do the without specifying the keys such that if ? – PradhanKamal Sep 11 '16 at 15:29
  • If they are fully dynamic in nature, how do you intend to handle data where not all fields are present in all objects? You've need to scan everything to build the full list of fields, before you can actually start writing the CSV. --- How will you handle objects where multiple fields are lists? Cartesian join? – Andreas Sep 11 '16 at 15:36
  • well i have to figure that out. but first i have to build the csv schema by extracting all the possible keys from json. – PradhanKamal Sep 11 '16 at 16:13
  • have a look at this website https://konklone.io/json/ and use the json i have provided above you will get a better understanding what i am trying to say. – PradhanKamal Sep 11 '16 at 16:27