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

  • Home
  • SEARCH
  • 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 8101247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:56:14+00:00 2026-06-05T22:56:14+00:00

I have a JSON like this { id:1, name:Jack, parent.id:2 } Note the dot

  • 0

I have a JSON like this

{ "id":1, "name":"Jack", "parent.id":2 }

Note the dot on “parent.id” property

Is it possible to map those JSON to the following classes ?

class Child {
    private int id;
    private String name;

    private Parent parent;

    //getter and setter methods
}

class Parent {
    private int id;
    private String name;

    //getter and setter methods
}

So the mapping result would be similar to following statements:

Parent parent = new Parent();
parent.setId(2);

Child child = new Child();
child.setId(1);
child.setName("Jack");
child.setParent(parent); // Here is the result
  • 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-05T22:56:16+00:00Added an answer on June 5, 2026 at 10:56 pm

    you can convert this

    { "id":1, "name":"Jack", "parent.id":2 }
    

    into this

    { "id":1, "name":"Jack", "parent": { "id":2 } }
    

    by using this

    // I'm using jQuery here 
    $.fn.serializeObject = function() {
      var arrayData, objectData;
      arrayData = this.serializeArray();
      objectData = {};
    
      $.each(arrayData, function() {
        var value;
    
        if (this.value != null) {
          value = this.value;
        } else {
          value = '';
        }
    
        // search for "parent.id" like attribute
        if (this.name.indexOf('.') != -1) {
          var attrs = this.name.split('.');
          var tx = objectData;
    
          for (var i = 0; i < attrs.length - 1; i++) {
            if (objectData[attrs[i]] == undefined)
              objectData[attrs[i]] = {};
            tx = objectData[attrs[i]];
          }
          tx[attrs[attrs.length - 1]] = value;
        } else {
          if (objectData[this.name] != null) {
            if (!objectData[this.name].push) {
              objectData[this.name] = [objectData[this.name]];
            }
    
            objectData[this.name].push(value);
          } else {
            objectData[this.name] = value;
          }
        }
      });
    
      return objectData;
    };
    

    and then you can serialize your code by using JSON.serialize().

    if you are using Jackson, then you can deserialize the JSON request string by doing any of these:

    1. create a custom Jackson deserialize module

    2. parse the JSON yourself

    public Child parseJackson(String jsonRequest) {
      // what we need
      ObjectMapper mapper;
      JsonNode root, parentNode;
    
      // your models
      Child child;
      Parent parent;
    
      // assign
      mapper = new ObjectMapper();
      root = mapper.readTree(jsonRequest); // deserialize JSON as tree
      parentNode = root.get("parent"); // get the "parent" branch
    
      // assign (again)
      child = mapper.readValue(root, Child.class);
      parent = mapper.readValue(parentNode, Parent.class);
    
      child.setParent(parent);
    
      return child;
    }
    

    the downside of this method is you have to parse for every single JsonRequest with nested objects and it will be messy when there’s a complex nested structure. If this is a problem, I suggest you do the #3

    3. create a custom Jackson ObjectMapper class to automate this process

    The idea is to build generic process for #2 so that it could handle any nested request.

    public class CustomObjectMapper extends ObjectMapper {
    
      // here's the method you need
      @Override
      public <T> T readValue(String src, Class<T> type)
          throws IOException, JsonParseException, JsonMappingException {
    
        JsonNode root = this.readTree(src);
        try {
          return readNestedValue(root, type);
        } catch (InstantiationException | IllegalAccessException | IOException
            | IllegalArgumentException | InvocationTargetException e) {
          return super.readValue(src, type);
        }
    
      }
    
      // if you're using Spring, I suggest you implement this method as well
      // since Spring's MappingJacksonHttpMessageConverter class will call 
      // this method.
      @Override
      public <T> T readValue(InputStream src, JavaType type)
          throws IOException, JsonParseException, JsonMappingException {
    
        JsonNode root = this.readTree(src);
        try {
          return readNestedValue(root, (Class<T>) type.getRawClass());
        } catch (InstantiationException | IllegalAccessException | IOException
            | IllegalArgumentException | InvocationTargetException e) {
          return super.readValue(src, type);
        }
    
      }
    
      // we need this to recursively scan the tree node
      protected <T> T readNestedValue(JsonNode root, Class<T> type)
          throws InstantiationException, IllegalAccessException, IOException,
            IllegalArgumentException, InvocationTargetException {
    
        // initialize the object use ObjectMapper's readValue
        T obj = super.readValue(root, type);
        Iterator it = root.getFieldNames();
        while (it.hasNext()) {
          String name = (String) it.next();
          String camelCaseName = name.substring(0, 1).toUpperCase() + name.substring(1);
          JsonNode node = root.get(name);
    
          Field f;
          try {
            f = type.getDeclaredField(name);
          } catch (NoSuchFieldException e) {
            f = findFieldInSuperClass(name, type.getSuperclass());
          }
          // if no field found then ignore
          if (f == null) continue; 
    
          Method getter, setter;
          try {
            getter = type.getMethod("get" + camelCaseName);
          } catch (NoSuchMethodException e) {
            getter = findGetterInSuperClass("get" + camelCaseName, type.getSuperclass());
          }
          // if no getter found or it has been assigned then ignore
          if (getter == null || getter.invoke(obj) != null) continue;
    
          try {
            setter = type.getMethod("set" + camelCaseName);
          } catch (NoSuchMethodException ex) {
            setter = findSetterInSuperClass("set" + camelCaseName, type.getSuperclass(), f.getType());
          }
          // if no setter found then ignore
          if (setter == null) continue;
    
          setter.invoke(obj, readNestedValue(node, f.getType()));
        }
    
        return obj;
      }
    
      // we need this to search for field in super class
      // since type.getDeclaredField() will only return fields that in the class
      // but not super class
      protected Field findFieldInSuperClass(String name, Class sClass) {
        if (sClass == null) return null;
        try {
          Field f = sClass.getDeclaredField(name);
          return f;
        } catch (NoSuchFieldException e) {
          return findFieldInSuperClass(name, sClass.getSuperclass());
        }
      }
    
      protected Method findGetterInSuperClass(String name, Class sClass) {
        if (sClass == null) return null;
        try {
          Method m = sClass.getMethod(name);
          return m;
        } catch (NoSuchMethodException e) {
          return findGetterInSuperClass(name, sClass.getSuperclass());
        }
      }
    
      protected Method findSetterInSuperClass(String name, Class sClass, Class type) {
        if (sClass == null) return null;
        try {
          Method m = sClass.getMethod(name, type);
          return m;
        } catch (NoSuchMethodException e) {
          return findSetterInSuperClass(name, sClass.getSuperclass(), type);
        }
      }
    }
    

    If you’re using Spring, then the final step is registering this class as Spring bean.

    <mvc:annotation-driven>
        <mvc:message-converters>
          <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
             <property name="objectMapper">
                <bean class="x.y.z.CustomObjectMapper"/>
             </property>
          </bean>
        </mvc:message-converters>
      </mvc:annotation-driven>
    

    with these set up you can easily use

    @RequestMapping("/saveChild.json")
    @ResponseBody
    public Child saveChild(@RequestBody Child child) {
      // do something with child
      return child;
    }
    

    Hope this helps 🙂

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

Sidebar

Related Questions

I have a REST service that returns a JSON like this: [{@id:123,name:Name}] and I'm
I have a json like this e.result = { 5474: { name: john, last:
I have a json output array like this { data: [ { name: Ben
I have JSON/JS like this that populates data: var settingDefs = []; settingDefs.push({ name:'responses',
I have a manifest.json file that looks like this: { name: Zend Debugger Extension,
I have a JSON object returned from server. It looks like this : {1:{id:1,name:autos},
I have a JSON response from Facebook liking like this. [ {uid=>123, name=>Test, is_app_user=>true},
I have a json string like this [ { name:sourabh, userid:soruabhbajaj, id:11, has_profile_image:0 },
I have JSON data like this: [{id:3,name:jason},{id:4,name:karen}] I want to build a table view
i have a json like this [{ item : { id:1 , name:abc }

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.