0

I want my RESTcontroller to get a JSONObject from a post request but when I send this through Postman:

{
    "collection":"StudentDB",
    "index":{
        "joinDate":"2022-12-12"
    }
}

It seems to be working, but the problem is that embedded JSONObjects seem to get cast into a LinkedHashmap and not be JSONObjects, so when I run this code:

@PostMapping
@RequestMapping(value="/query",consumes="application/json")
public ResponseEntity query( @RequestBody JSONObject query) {
    System.out.println(query.get("index").getClass());
}

Output is:

class java.util.LinkedHashMap

What could be causing this? Is there another way I could do this?

4
  • what is JSONObjects? Is this a custom class? Commented May 21, 2022 at 19:25
  • its from org.json.simple.JSONObject Commented May 21, 2022 at 19:26
  • is there a specific reason for using this over using a custom domain object? Commented May 21, 2022 at 19:48
  • im making a document based database, the data is saved in a JSON format. do you think there is a better way ? Commented May 21, 2022 at 20:54

1 Answer 1

1

It looks like the reason why you get java.util.LinkedHashMap value for index key is because under the hood JSONObject uses a Map to store it's key-value pairs.

So each key-value pair(for example, "collection" : "StudentDB") is actually stored in this map that's wrapped in a JSONObject.

And when you wrote "index" : { key-value pairs here }, you essentially told JSONObject that you want to create another Map(for key-value pairs that will be inside of { }) that will be serving as a value for index key.

As for why LinkedHashMap is used here, the reason for that is because JSONObject needs to preserve the ordering of its elements, so LinkedHashMap does just that.

The solution to your problem depends on what you're exactly trying to achieve here. The logic of the program seems to be okay, so hopefully you'll figure out what to do about it based on what I've written here.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, but when i parse a jsonobject using JSONparser that doesnt happen. all through my code i use json objects have JSONObjects embedded , and not maps. why would the restcontroller receive the embedded objects as maps ? . i uses instanceOf to find nested JSONObjects, and having them as maps messes things up. any idea of a solution ?
Unfortunately, I cannot give an answer as to why it happens only in rest controllers and doesn't happen anywhere else. Only a good look at the code could help here. But an easy solution that comes to mind that can help you to go from LinkedHashMap to JSONObject is to create JSONObject from Map using the new operator. There is a constructor in JSONObject that accepts Map as its parameter. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.