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 8859581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:06:09+00:00 2026-06-14T15:06:09+00:00

I have a JSON file which can have multiple types. For example: { dog:

  • 0

I have a JSON file which can have multiple types.

For example:

{
    "dog": {
        "owner" : "John Smith",
        "name" : "Rex",
        "toys" : {
            "chewtoy" : "5",
            "bone" : "1"
        }
    },
    "person": {
        "name" : "John Doe",
        "address" : "23 Somewhere Lane"
    }
    // Further examples of dogs and people, and a few other types.
}

I want to parse these into objects. ie. I want to create a Dog object with owner/name/toys attributes, and person with name/address attributes, and use Jackson to read through and create objects out of them.

The ordering matters – I need to know that Rex came before John Doe, for example. I would prefer to do with a stream like approach (ie. read and parse Rex into the Dog object, do something with it, then discard it, then move onto to John Doe). So I need a stream based approach.

I can’t figure out how to use both the stream reading API (to go through in order) and the ObjectMapper interface (in order to create Java objects out of JSON) to accomplish this.

  • 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-14T15:06:10+00:00Added an answer on June 14, 2026 at 3:06 pm

    To do this, you need to use an object mapper with your factory

    import org.codehaus.jackson.JsonFactory;
    import org.codehaus.jackson.JsonParser;
    import org.codehaus.jackson.JsonProcessingException;
    import org.codehaus.jackson.map.ObjectMapper;
    ...
    private static ObjectMapper mapper = new ObjectMapper();
    private static JsonFactory factory = mapper.getJsonFactory();
    

    Then create a parser for the input.

    JsonParser parser = factory.createJsonParser(in);
    

    Now you can mix calls to parser.nextToken() and calls to parser.readValueAs(Class c). Here is an example that gets the classes from a map:

    Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
    classMap.put("dog", Dog.class);
    classMap.put("person", Person.class);
    
    InputStream in = null;
    JsonParser parser = null;
    List<Object> results = new ArrayList<Object>();
    try {
      in = this.getClass().getResourceAsStream("input.json");
      parser = factory.createJsonParser(in);
      parser.nextToken();// JsonToken.START_OBJECT
      JsonToken token = null;
      while( (token = parser.nextToken()) == JsonToken.FIELD_NAME ) {
        String name = parser.getText();
        parser.nextToken(); // JsonToken.START_OBJECT
        results.add(parser.readValueAs(classMap.get(name)));
      }
      // ASSERT: token = JsonToken.END_OBJECT
    }
    finally {
      IOUtils.closeQuietly(in);
      try {
        parser.close();
      }
      catch( Exception e ) {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON string (external file) which has an element which can either
I have parse json file which contain more than hundred location name, there latitude
I have a JSON file which contains a pound symbol in it. This json
I have a JSON file which contains HTML content. I want to load it
I have a flash file which loads data from JSON and parses it. It
I have a set of offline data in JSON file which i kept in
So I have a JSON file, which is basically an ATOM XML encoded into
I have a local url where i can retrieve a json file. I also
I have a json.php file which delivers results like this: { markers: [ {'a1_id':4213CK58,
I have a Web service which returns a JSON file, and I'm required 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.