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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:39:16+00:00 2026-06-11T11:39:16+00:00

I am parsing JSON and am having difficulty with one structure that can have

  • 0

I am parsing JSON and am having difficulty with one structure that can have one of three forms. In my case it could be zero-dimensional, one-dimensional or two-dimensional. Is there some way I can inspect the JSON on the fly to determine which one it is? Or perhaps consume it anyway and work out what it is afterwards.

The structures look like this and can be embedded in other structures.

"details":{
    "Product":"A zero-dimensional Product"
},

"details":{
    "Product":"A one-dimensional Product",
    "Dimensions": [ "Size" ],
    "Labels": [ "XS", "S", "M", "L" ]
},

"details":{
    "Product":"A two-dimensional Product",
    "Dimensions": [ "Size", "Fit" ],
    "Labels": [[ "XS", "S", "M", "L" ],[ "26", "28", "30", "32" ]]
}

What I may be looking for is a generic class that Jackson will always match with.

Something like translating:

{
"SomeField": "SomeValue",
...
 "details":{
  ...
 }
}

Into:

class MyClass {
  String SomeField;
  ...
  AClass details;
}

Is there a class AClass I can define that could be a universal recipient for any JSON structure or array?

  • 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-11T11:39:17+00:00Added an answer on June 11, 2026 at 11:39 am

    Thanks to Eric’s comment pointing me to programmerbruce I managed to crack it. Here’s the code I used (cut down to simplify).

    public static class Info {
      @JsonProperty("Product")
      public String product;
      // Empty in the 0d version, One entry in the 1d version, two entries in the 2d version.
      @JsonProperty("Dimensions")
      public String[] dimensions;
    
    }
    
    public static class Info0d extends Info {
    }
    
    public static class Info1d extends Info {
      @JsonProperty("Labels")
      public String[] labels;
    }
    
    public static class Info2d extends Info {
      @JsonProperty("Labels")
      public String[][] labels;
    }
    
    public static class InfoDeserializer extends StdDeserializer<Info> {
      public InfoDeserializer() {
        super(Info.class);
      }
    
      @Override
      public Info deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        Class<? extends Info> variantInfoClass = null;
        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
        ObjectNode root = (ObjectNode) mapper.readTree(jp);
        // Inspect the `diemnsions` field to decide what to expect.
        JsonNode dimensions = root.get("Dimensions");
        if ( dimensions == null ) {
          variantInfoClass = Info0d.class;
        } else {
          switch ( dimensions.size() ) {
            case 1:
              variantInfoClass = Info1d.class;
              break;
    
            case 2:
              variantInfoClass = Info2d.class;
              break;
          }
        }
        if (variantInfoClass == null) {
          return null;
        }
        return mapper.readValue(root, variantInfoClass);
      }
    }
    

    And to install this in the ObjectMapper:

    // Register the special deserializer.
    InfoDeserializer deserializer = new InfoDeserializer();
    SimpleModule module = new SimpleModule("PolymorphicInfoDeserializerModule", new Version(1, 0, 0, null));
    module.addDeserializer(Info.class, deserializer);
    mapper.registerModule(module);
    factory = new JsonFactory(mapper);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble parsing a JSON that I obtain from my DB to
Hello friends i am having problem in parsing jSON web services data I have
I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
I've been having some troubles with parsing JSON that is received with WebSocket (original
I'm having some difficulties parsing a JSON package in Android. I currently have everything
I'm having a problem on parsing a JSON from Google Places. I have the
I'm working with django-piston and parsing that JSON output in Objective-C, but having some
I am having great difficulty in successfully parsing a JSON file to use within
I am having trouble parsing my JSON which i get from javascript. The format
I'm having a problem parsing a JSON file in AS3. Im trying to parse

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.