0

With this code, I am trying read all of the data from a JSON file, parse it with Jackson, remove unwanted fields, and put the remaining fields into a new file.

So far, my code works, except ObjectMapper.readTree only reads the first object in the JSON file. I know I need to loop through each object in the file, but I don't know how to iterate through the tree.

Thank you in advance,

David Riley

public class App {
    public static void main(String[] args) throws IOException {
        File file = new File("exampleFile.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

            JsonNode root = mapper.readTree(file);


            JsonCleaner jsonCleaner = new JsonCleaner(root, (node) -> node != null);// isNumber());
            JsonNode result = jsonCleaner.removeAll();


            File cardFile = new File("masterCardList.json");
            FileWriter fileWriter;
            try {
                cardFile.createNewFile();
                fileWriter = new FileWriter(cardFile, true);
                fileWriter.write(result.toString() + ",");
                fileWriter.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
      }
}


public class JsonCleaner {
    private final JsonNode root;
    private final Predicate<JsonNode> toRemoveCondition;

    JsonCleaner(JsonNode node, Predicate<JsonNode> toRemoveCondition) {
        this.root = Objects.requireNonNull(node);
        this.toRemoveCondition = Objects.requireNonNull(toRemoveCondition);
    }

    public JsonNode removeAll() {
        process(root);
        return root;
    }



    private void process(JsonNode node) {
        if (node.isObject()) {
            ObjectNode object = (ObjectNode) node;
            Iterator<Map.Entry<String, JsonNode>> fields = object.fields();
            while (fields.hasNext()) {
                Map.Entry<String, JsonNode> field = fields.next();
                JsonNode valueToCheck = field.getValue();
                // System.out.println(field.getKey());
                if (field.getKey() == "artist" || field.getKey() == "colors" ||
                            field.getKey() == "multiverseId" || field.getKey() == "scryfallId" ||
                            field.getKey() == "manaCost" || field.getKey() == "name" || field.getKey() == "rarity" ||
                            field.getKey() == "type" || (field.getKey() == "identifiers")) {
                    process(valueToCheck);
                } else {
                    fields.remove();
                }
            }
        } else if (node.isArray()) {
            ArrayNode array = (ArrayNode) node;
            array.elements().forEachRemaining(this::process);
        }
    }
}
Gatene
  • 47
  • 5
  • Are you sure the file is valid json? – Mr R Mar 19 '22 at 09:57
  • Yes, it has over 500 objects in it. My program only reads the very first object in it, and the file my program creates, only has the first object in it too. – Gatene Mar 19 '22 at 10:03
  • Perhaps provide a sample of the file - is it possible root is a JSONArray?? – Mr R Mar 19 '22 at 10:45
  • It is very large. One object has over 50 key,value pairs in it, and there are over 500 entries. I checked the first few entries, and they are correct. Each object begins with and ends with curly braces, and are comma-separated. It is a bit over 3 MB in size. – Gatene Mar 19 '22 at 10:53
  • This https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects seems to suggest you may be doing the wrong thing, but without a minimal input sample it's a bit hard to tell. – Mr R Mar 19 '22 at 10:57

0 Answers0