skip to Main Content

I need to print everything in a JSON file using Jackson in Java. I am 90% of the way there. Here is my Json File:

{
    "my-data": {
        "phone-number": "5555555555",
        "userId": "1234",
        "version": "2",
        "more-data": {
            "transaction-data": [
                {
                    "Merchant": "Walmart",
                    "authorized": "true",
                    "amount": "7.50",
                    "is_card_expired": false,
                    "credit_limit": "2000"
                }
            ]
        }
    }
}

I am able to print everything that has a key, but need help cleanly dealing with the array inside transaction-data. Notice that is an array with no key. Therefore, it is tricky to get inside, and my iterator (Iterator<Map.Entry<String, JsonNode>> Iterator = node.fields()) turns up empty. I cannot use node.elements(), because I need to get the key for everything, even if it is not something to print out. To print everything, my result should look something like

"phone-number": "5555555555"
"userId": "1234"
"version": "2"
"Merchant": "Walmart",
"authorized": "true",
"amount": "7.50",
"is_card_expired": false,
"credit_limit": "2000"

The best I have been able to come up with is after declaring my interator, I check if it hasNext(), and if not, then I create another iterator of elements (Iterator<JsonNode> iterator2 = node.elements();). While that has next I recursively call my method on each of the nodes inside that. This works, but I am not sure this is the best solution. How would you cleanly iterate through/traverse my JSON file to hit each and every node, regardless of if it is an object, an array, or a value node, and regardless of whether or not a key exists (if a key does not exist then I need not print it out, I just need to enter that container and print out the items inside of it

2

Answers


  1. This can be solved using standard recursive approach.
    Where given entry is either a Object or Array of Objects.

    Following link has exact same problem statement as yours:

    java using JSONObject to loop recursive through any jsondata

    Hope this helps. Thanks.

    Login or Signup to reply.
  2. You can quite easily do this with a JSON parser:

    JsonParser parser = new JsonFactory().createParser(json);
    JsonToken token = parser.nextToken();
    while (token != null) {
        switch (token) {
            case VALUE_FALSE:
            case VALUE_TRUE:
                System.out.printf(""%s": %b%n", parser.currentName(), parser.getBooleanValue());
                break;
            case VALUE_NULL:
                System.out.printf(""%s": null%n", parser.currentName());
                break;
            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT:
                System.out.printf(""%s": %d%n", parser.currentName(), parser.getNumberValue());
                break;
            case VALUE_STRING:
                System.out.printf(""%s": "%s"%n", parser.currentName(), parser.getText());
                break;
            default:
                // ignore
                break;
        }
        token = parser.nextToken();
    }
    

    The parser has a method called currentValue() but that doesn’t work in the above example, it always returns null.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search