json_csv.JSON2CSV.java Source code

Java tutorial

Introduction

Here is the source code for json_csv.JSON2CSV.java

Source

package json_csv;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.io.FileUtils;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSON2CSV {
    //instance variables
    Recipes recipes = new Recipes();
    BufferedWriter ibw;
    BufferedWriter rbw;
    BufferedWriter cbw;

    private static final String CSV_SEPARATOR = ",";

    public static void main(String myHelpers[]) {

        // These code snippets use an open-source library. http://unirest.io/java
        ObjectMapper mapper = new ObjectMapper();
        JSON2CSV run = new JSON2CSV();

        try {

            run.ibw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ingredient.csv"), "UTF-8"));
            run.rbw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("recipe.csv"), "UTF-8"));
            run.cbw = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream("cuisineRecipe.csv"), "UTF-8"));

            for (int i = 0; i < 100; i++) {

                // These code snippets use an open-source library. http://unirest.io/java
                HttpResponse<JsonNode> response = Unirest.get(
                        "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/random?limitLicense=false&number=100")
                        .header("X-Mashape-Key", "dkK9bOKEkNmshOLDuw9rHq59F0Twp1fvyxcjsn99AoUm6XvMfC")
                        .header("Accept", "application/json").asJson();

                // retrieve the parsed JSONObject from the response
                JSONObject myObj = response.getBody().getObject();

                JSONArray results = myObj.getJSONArray("recipes");

                run.parseRecipe(results);

                run.printRecipeToCSV();

                run.printCuisineLookupTable(run.recipes);
            }

            run.rbw.flush();
            run.rbw.close();

            run.ibw.flush();
            run.ibw.close();

            run.cbw.flush();
            run.cbw.close();

        } catch (UnirestException ue) {

            ue.printStackTrace();

        } catch (JSONException e) {

            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void printRecipeToCSV() {

        Recipe recipe = null;

        try {

            System.out.println(recipes.getRecipes().size());

            for (int i = 0; i < this.recipes.getRecipes().size(); i++) {

                recipe = this.recipes.getRecipes().get(i);
                StringBuffer oneLine = new StringBuffer();

                oneLine.append(recipe.getId());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append((recipe.getTitle().replace(",", " ")));
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getAggregateLikes());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getSourceUrl());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getReadyInMinutes());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getServings());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getImageType());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getImage());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getSourceName());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getVeryPopular());

                printIngredientToCSV(recipe, this.ibw);
                printCuisineToCSV(recipe, this.cbw);

                this.rbw.write(oneLine.toString());
                this.rbw.newLine();

            }
        } catch (UnsupportedEncodingException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

    }

    public void printCuisineLookupTable(Recipes recipes) {
        Recipes newRecipes = recipes;
        List<String> printLookupTable = newRecipes.getCuisineMap();
        BufferedWriter ctw = null;

        try {

            StringBuffer oneLine = new StringBuffer();
            ctw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("cuisine.csv"), "UTF-8"));

            //iterates through the map and print
            for (String cuisine : printLookupTable) {

                ctw.write(cuisine);
                ctw.newLine();
            }

            ctw.flush();
            ctw.close();
        } catch (UnsupportedEncodingException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }

    public void printCuisineToCSV(Recipe recipe, BufferedWriter cbw) {
        BufferedWriter newCBW = cbw;
        Recipe newRecipe = recipe;
        Cuisine newCuisine = null;
        StringBuffer oneLine = null;

        try {
            System.out.println(newRecipe.getExtendedIngredients().size());

            for (int i = 0; i < newRecipe.getCuisine().size(); i++) {

                newCuisine = newRecipe.getCuisine().get(i);
                oneLine = new StringBuffer();

                oneLine.append(newCuisine.getCuisineType());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(newCuisine.getRecipeID());
                oneLine.append(CSV_SEPARATOR);

                cbw.write(oneLine.toString());
                cbw.newLine();

            }
        } catch (UnsupportedEncodingException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

    }

    public void printIngredientToCSV(Recipe recipe, BufferedWriter ibw) {

        Recipe newRecipe = recipe;
        Ingredient ingredient = null;
        StringBuffer oneLine = null;
        BufferedWriter newIbw = ibw;

        //String[] header = {"INGREDIENT_ID", "AISLE", "NAME", "AMOUNT", "ORIGINAL_STRING", "IMAGE", "UNIT", "UNIT_SHORT",
        //        "UNIT_LONG", "RECIPE_ID"};

        try {
            System.out.println(newRecipe.getExtendedIngredients().size());

            for (int i = 0; i < newRecipe.getExtendedIngredients().size(); i++) {

                ingredient = newRecipe.getExtendedIngredients().get(i);
                oneLine = new StringBuffer();

                oneLine.append(ingredient.getIngredientID());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(ingredient.getAisle().replace(",", ";"));
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(ingredient.getName());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(ingredient.getAmount());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append((ingredient.getOriginalString().replace(",", " ")));
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(ingredient.getImage());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(ingredient.getUnit());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(ingredient.getUnitShort());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(ingredient.getUnitLong());
                oneLine.append(CSV_SEPARATOR);
                oneLine.append(recipe.getId());

                ibw.write(oneLine.toString());
                ibw.newLine();

            }
        } catch (UnsupportedEncodingException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

    }

    public void parseRecipe(JSONArray results) {

        try {
            for (int i = 0; i < results.length(); i++) {

                JSONObject obj = results.getJSONObject(i);

                String id = ((obj.has("id") && !obj.isNull("id")) ? obj.getString("id") : "");

                String title = ((obj.has("title") && !obj.isNull("title")) ? obj.getString("title") : "");
                String aggregateLike = ((obj.has("aggregateLikes") && !obj.isNull("aggregateLikes"))
                        ? obj.getString("aggregateLikes")
                        : "");
                String sourceUrl = ((obj.has("sourceUrl") && !obj.isNull("sourceUrl")) ? obj.getString("sourceUrl")
                        : "");
                String readyInMinutes = ((obj.has("readyInMinutes") && !obj.isNull("readyInMinutes"))
                        ? obj.getString("readyInMinutes")
                        : "");
                String servings = ((obj.has("servings") && !obj.isNull("servings")) ? obj.getString("servings")
                        : "");
                String imageType = ((obj.has("imageType") && !obj.isNull("imageType")) ? obj.getString("imageType")
                        : "");
                String image = ((obj.has("image") && !obj.isNull("image")) ? obj.getString("image") : "");
                String sourceName = ((obj.has("sourceName") && !obj.isNull("sourceName"))
                        ? obj.getString("sourceName")
                        : "");
                String veryPopular = ((obj.has("veryPopular") && !obj.isNull("veryPopular"))
                        ? obj.getString("veryPopular")
                        : "");
                String cuisineString = ((obj.has("cuisines") && !obj.isNull("cuisines")) ? obj.getString("cuisines")
                        : "");

                String extendedIngredientString = ((obj.has("extendedIngredients")
                        && !obj.isNull("extendedIngredients")) ? obj.getString("extendedIngredients") : "");

                Recipe newRecipe = new Recipe(id, title, aggregateLike, sourceUrl, readyInMinutes, servings,
                        imageType, image, sourceName, veryPopular);

                this.recipes.getRecipes().add(newRecipe);
                this.recipes.getCuisineMap();

                JSONArray cuisines = new JSONArray(cuisineString);
                JSONArray extendedIngredients = new JSONArray(extendedIngredientString);

                parseCuisine(newRecipe, cuisines);
                parseIngredient(newRecipe, extendedIngredients);
            }

        } catch (JSONException je) {
            je.printStackTrace();
        }
    }

    public void parseIngredient(Recipe recipe, JSONArray ingredients) {
        try {
            for (int i = 0; i < ingredients.length(); i++) {

                JSONObject obj = ingredients.getJSONObject(i);

                String id = ((obj.has("id") && !(obj.isNull("id")) ? obj.getString("id") : ""));
                String aisle = ((obj.has("aisle") && !(obj.isNull("aisle")) ? obj.getString("aisle") : ""));
                String name = ((obj.has("name") && !(obj.isNull("name")) ? obj.getString("name") : ""));
                String amount = ((obj.has("amount") && !(obj.isNull("amount")) ? obj.getString("amount") : ""));
                String originalString = ((obj.has("originalString") && !(obj.isNull("originalString"))
                        ? obj.getString("originalString")
                        : ""));
                String unit = ((obj.has("unit") && !(obj.isNull("unit")) ? obj.getString("unit") : ""));
                String unitShort = ((obj.has("unitShort") && !(obj.isNull("unitShort")) ? obj.getString("unitShort")
                        : ""));
                String unitLong = ((obj.has("unitLong") && !(obj.isNull("unitLong")) ? obj.getString("unitLong")
                        : ""));
                String image = ((obj.has("image") && !(obj.isNull("image")) ? obj.getString("image") : ""));

                //TODO: add to object
                Ingredient ingredient = new Ingredient(recipe.getId(), id, aisle, image, name, amount,
                        originalString, unit, unitShort, unitLong);

                recipe.getExtendedIngredients().add(ingredient);
            }

        } catch (JSONException je) {
            je.printStackTrace();
        }

    }

    public void parseCuisine(Recipe recipe, JSONArray cuisine) {
        try {
            for (int i = 0; i < cuisine.length(); i++) {

                String cuisineString = cuisine.getString(i);

                Cuisine newCuisine = new Cuisine(recipe.getId(), cuisineString);

                recipe.getCuisine().add(newCuisine);
            }

        } catch (JSONException je) {
            je.printStackTrace();
        }
    }
}