Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8261775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:31:49+00:00 2026-06-08T03:31:49+00:00

I was wondering if there is a correct way to parse a JSON file

  • 0

I was wondering if there is a “correct” way to parse a JSON file using Jackson where the JSON file contains a property that is huge without loading the entire stream into memory. I need to keep the memory low since it’s an Android app. Am not asking here how to Android: Parsing large JSON file but rather one property is really large and the others don’t matter.

For instance, let’s say i have the following :

{
    "filename": "afilename.jpg",
    "data": "**Huge data here, about 20Mb base64 string**",
    "mime": "mimeType",
    "otherProperties": "..."
}

The data property could be extracted to a new file if needed (via an outputstream or other meanings) but i don’t manage to achieve this using Jackson. Am open to use other libraries i just thought jackson would be ideal thanks to it’s streaming API.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:31:50+00:00Added an answer on June 8, 2026 at 3:31 am

    Finally I manage to recover my huge data like this, where in is an inputstream over the json file i want to parse the data from and out is the file where am gonna write my data to:

    public boolean extrationContenuDocument(FileInputStream in, FileOutputStream out, FileInfo info) 
    throws JsonParseException, IOException {
    
        SerializedString keyDocContent = new SerializedString("data");
        boolean isDone = false;
    
        JsonParser jp = this.jsonFactory.createJsonParser(in);
    
        // Let's move our inputstream cursor until the 'data' property is found
        while (!jp.nextFieldName(keyDocContent)) {
            Log.v("Traitement JSON", "Searching for 'data' property ...");
        }
    
        // Found it? Ok, move the inputstream cursor until the begining of it's
        // content
        JsonToken current = jp.nextToken();
    
        // if the current token is not String value it means u didn't found the
        // 'data' property or it's content is not a correct => stop
        if (current == JsonToken.VALUE_STRING) {
            Log.v("Traitement JSON", "Property 'data' found");
    
            // Here it gets a little tricky cause if the file is not big enough
            // all the content of the 'data' property could be read directly
            // insted of using this
            if (info.getSize() > TAILLE_MIN_PETIT_FICHER) {
                Log.v("Traitement JSON", "the content of 'data' is too big to be read directly -> using buffered reading");
    
                // JsonParser uses a buffer to read, there is some data that
                // could have been read by it, i need to fetch it
                ByteArrayOutputStream debutDocStream = new ByteArrayOutputStream();
                int premierePartieRead = jp.releaseBuffered(debutDocStream);
                byte[] debutDoc = debutDocStream.toByteArray();
    
                // Write the head of the content of the 'data' property, this is
                // actually what as read from the inputstream by the JsonParser
                // when did jp.nextToken()
                Log.v("Traitement JSON", "Write the head");
                out.write(debutDoc);
    
                // Now we need to write the rest until we find the tail of the
                // content of the 'data' property
                Log.v("Traitement JSON", "Write the middle");
    
                // So i prepare a buffer to continue reading the inputstream
                byte[] buffer = new byte[TAILLE_BUFFER_GROS_FICHER];
    
                // The escape char that determines where to stop reading will be "
                byte endChar = (byte) '"';
    
                // Fetch me some bytes from the inputstream
                int bytesRead = in.read(buffer);
                int bytesBeforeEndChar = 0;
    
                int deuxiemePartieRead = 0;
                boolean isDocContentFin = false;
    
                // Are we at the end of the 'data' property? Keep writing the
                // content of the 'data' property if it's not the case
                while ((bytesRead > 0) && !isDocContentFin) {
                    bytesBeforeEndChar = 0;
    
                    // Since am using a buffer the escape char could be in the
                    // middle of it, gotta look if it is
                    for (byte b : buffer) {
                        if (b != endChar) {
                            bytesBeforeEndChar++;
                        } else {
                            isDocContentFin = true;
                            break;
                        }
                    }
    
                    if (bytesRead > bytesBeforeEndChar) {
                        Log.v("Traitement JSON", "Write the tail");
                        out.write(buffer, 0, bytesBeforeEndChar);
                        deuxiemePartieRead += bytesBeforeEndChar;
                    } else {
                        out.write(buffer, 0, bytesRead);
                        deuxiemePartieRead += bytesRead;
                    }
    
                    bytesRead = in.read(buffer);
                }
    
                Log.v("Traitement JSON", "Bytes read: " + (premierePartieRead + deuxiemePartieRead) + " (" + premierePartieRead + " head,"
                        + deuxiemePartieRead + " tail)");
                isDone = true;
            } else {
                Log.v("Traitement JSON", "File is small enough to be read directly");
                String contenuFichier = jp.getText();
                out.write(contenuFichier.getBytes());
                isDone = true;
            }
        } else {
            throw new JsonParseException("The property " + keyDocContent.getValue() + " couldn't be found in the Json Stream.", null);
        }
        jp.close();
    
        return isDone;
    }
    

    It’s not pretty, but works like a charm! @staxman let me know what you think.

    Edit :


    This is now an implemented feature , see : https://github.com/FasterXML/jackson-core/issues/14
    and JsonParser.readBinaryValue()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started using WebSVN and I'm wondering if there is a correct/propper way
I am wondering if there is a correct way to implement the anchor tag
Wondering if there is any way to get the lambda expressions that result from
Wondering if there is a good way to generate temporary URLs that expire in
I'm wondering what the correct way to reset/unset the CSS behavior property is for
I am new to programming, and am wondering if there is a correct way
I've been experimenting with xhtml and now I'm wondering that is there a valid/correct
Wondering if there is any tool that can help me to detect a pronoun's
just wondering if there is a way to reduce the amount of code needed
Just wondering is there any way I can check whether the url links to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.