Streaming two json files in an order using JsonParserSequence

This post explains how to stream two files in a particular order

Main Class


import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.util.JsonParserSequence;

public class ProcessingDemo3 {
    public static void main(String[] args) throws JsonParseException, IOException {
        File file1 = new File("jsondata.json");
        File file2 = new File("jsondata1.json");
        
        JsonFactory jsonFactory = new JsonFactory();
        
        JsonParser jsonParser1 = jsonFactory.createParser(file1);
        JsonParser jsonParser2 = jsonFactory.createParser(file2);
        
        JsonParserSequence jsonParserSequence = JsonParserSequence.createFlattened(jsonParser1, jsonParser2);
        
        JsonToken jsonToken = jsonParserSequence.nextToken();
        while(jsonToken != null) {
            switch(jsonToken) {
                case FIELD_NAME: System.out.println("Key field: " + jsonParserSequence.getText());
                                    break;
                case VALUE_FALSE:
                case VALUE_NULL:
                case VALUE_NUMBER_FLOAT:
                case VALUE_NUMBER_INT:
                case VALUE_STRING:
                case VALUE_TRUE: System.out.println("Key value: " + jsonParserSequence.getText());
                            break;
            }
            jsonToken = jsonParserSequence.nextToken();
        }
        jsonParserSequence.close();
    }
}

JsonParserSequence is a helper class which allows developers to create a parser containing two sub-parsers placed in a particular sequence. We can create a sequence using the below method

public static JsonParserSequence createFlattened(JsonParser first, JsonParser second)

Input

jsondata.json


{
    "firstName": "Duke",
    "lastName": "Java",
    "age": 18,
    "streetAddress": "100 Internet Dr",
    "city": "JavaTown",
    "state": "JA",
    "postalCode": "12345",
    "phoneNumbers": [
        { "Mobile": "111-111-1111" },
        { "Home": "222-222-2222" }
    ]
}

jsondata1.json


{
    "firstName":"John",
    "lastName":"McClane",
    "age":"28",
    "address":{
        "street":"street1",
        "city":"city1",
        "state":"state1",
        "country":"country1",
        "postalCode":"12345"
        },
    "Phones": [
            {"Mobile":"111-111-1111"},
            {"Home":"222-222-2222"}
        ]
}

Output

Key field: firstName
Key value: Duke
Key field: lastName
Key value: Java
Key field: age
Key value: 18
Key field: streetAddress
Key value: 100 Internet Dr
Key field: city
Key value: JavaTown
Key field: state
Key value: JA
Key field: postalCode
Key value: 12345
Key field: phoneNumbers
Key field: Mobile
Key value: 111-111-1111
Key field: Home
Key value: 222-222-2222
Key field: firstName
Key value: John
Key field: lastName
Key value: McClane
Key field: age
Key value: 28
Key field: address
Key field: street
Key value: street1
Key field: city
Key value: city1
Key field: state
Key value: state1
Key field: country
Key value: country1
Key field: postalCode
Key value: 12345
Key field: Phones
Key field: Mobile
Key value: 111-111-1111
Key field: Home
Key value: 222-222-2222

Leave a Reply