Quick revision
The short answer
org.json's JSONObject stores its keys in a HashMap, so the order of keys in the output is not the order we put them in and can change between versions. Its number handling has also changed between releases, and many versions print numbers through a routine that trims trailing zeros, so a BigDecimal of 10.10 can come out as 10.1. Clients should not depend on key order, but string comparisons and signatures do, so for typed responses we should use a binding library such as Jackson or JSON-B with records, which gives us a fixed property order and exact control over numbers.
What to remember
In this answer, we saw that org.json's JSONObject is backed by a HashMap, so key order follows hashing rather than insertion, and that its number formatting has changed between versions and may trim 10.10 to 10.1. Key order should not matter to a correct client, but string comparisons, caches and signatures do depend on it, and a lost scale can be a real bug for money. We replaced hand-built responses with records serialized by Jackson or JSON-B, which fix the property order and keep BigDecimal exact, and kept org.json only for small isolated uses with its version pinned and its licence checked.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
org.json's JSONObject stores its keys in a HashMap, so the order of keys in the output is not the order we put them in and can change between versions. Its number handling has also changed between releases, and many versions print numbers through a routine that trims trailing zeros, so a BigDecimal of 10.10 can come out as 10.1. Clients should not depend on key order, but string comparisons and signatures do, so for typed responses we should use a binding library such as Jackson or JSON-B with records, which gives us a fixed property order and exact control over numbers.
Why this question is asked
This is a typical problem in older code: a response is assembled by hand with a generic JSON object, it works for years, and then a library upgrade or a new client exposes two assumptions nobody wrote down. One is that keys come out in the order we added them; the other is that a number keeps the exact form we gave it. Interviewers ask this to see whether we can tell which complaint is a real bug and which is a client depending on something JSON never promised. In this answer, we will recap what org.json is, reproduce both effects, decide which ones matter, and then rewrite the response with a data binding library.
1A Quick Recap of org.json
org.json is one of the oldest JSON libraries for Java. It gives us two main classes: JSONObject, which behaves like a map from string keys to values, and JSONArray, which behaves like a list. We build a document by calling put() repeatedly and turn it into text with toString(). There is no data binding, meaning no automatic mapping between our own classes and JSON; every field is copied by hand.
Inside, a JSONObject keeps its entries in a java.util.HashMap. A HashMap places entries by the hash of each key, not by when they were added, so iterating over it, which is what toString() does, returns keys in an order that depends on the keys themselves and on the map's internal size.
2Reproducing the Two Complaints
Here is code like the one in the question, with a few more keys so the reordering is visible.
| Complaint | Cause | Real problem? |
|---|---|---|
| Keys in a different order | JSONObject is backed by a HashMap | Only for clients that compare strings |
| 10.10 became 10.1 | number formatting trims zeros (version dependent) | Yes, if the client shows or signs the text |
| Order changed after an upgrade | different HashMap size or library version | Same as the first row |
Notice both problems in one line. The keys came out in hash order. And the amount lost its trailing zero, because many versions of org.json turn a number into text with a helper that removes trailing zeros after the decimal point. Different releases have changed how numbers are stored and printed, including when a BigDecimal is kept as it is, when a value becomes a Double, and how very large or very small numbers are written, so the exact output depends on which version the service uses. The list, by contrast, is fine: put() with a Collection wraps it in a JSONArray and keeps its order.
3Which Complaint Is a Real Bug?
The JSON specification (RFC 8259) says an object is an unordered collection of name and value pairs. A well-written client parses the JSON and looks fields up by name, so key order should never matter to it. If a client breaks when keys move, the client has a bug.
But there are honest reasons for clients to care. Tests that compare the response with an expected string fail. Caching layers that hash the body see a new body. And, most seriously, a signature computed over the JSON text, such as an HMAC that a partner checks, breaks when the text changes even though the data did not. For signatures, the right answer is to sign a canonical form of the JSON (RFC 8785 describes one) or to sign the exact bytes sent and never re-serialize them.
The number change is more often a real bug. 10.10 and 10.1 are the same numeric value, so a client that parses into BigDecimal and compares with compareTo() is fine. But a client that shows the text, a receipt that must say 10.10 EUR, or a client that compares with BigDecimal.equals(), which also compares the scale, sees a difference. For money, the safest contract is often to send the amount as a string, or as an integer in minor units such as 1010 cents.
4Rewriting the Response With Data Binding
The lasting fix is to stop assembling responses by hand. With a binding library, the response becomes a type, and the type decides the order and the format.
Notice what changed. Jackson writes a BigDecimal exactly as BigDecimal.toString() would, so the scale of 2 is kept and the amount stays 10.10; turning on WRITE_BIGDECIMAL_AS_PLAIN also stops very small or large values from appearing in scientific notation. The order is fixed by the record's components, and the annotation makes it explicit. In Spring Boot we would simply return the record from the controller and let the auto-configured mapper do the rest. JSON-B with Yasson works the same way, with @JsonbPropertyOrder for the order and @JsonbNumberFormat for formatting.
Before switching, we would capture the current responses in tests so that every intended difference, such as 10.10 replacing 10.1, is a decision and not a surprise for a client that has adapted to the old output.
5When org.json Is Still Acceptable
org.json is not dangerous in itself; it is simply a poor fit for typed service responses. For a small script, a one-off migration tool or a test helper that inspects a document, it is quick and has no dependencies. If we keep it, we keep it in isolated places, pin its version so formatting does not change under us, and check its licence against company policy: older releases used the JSON License with its unusual 'Good, not Evil' clause, which many legal teams reject, while newer releases moved to a public domain dedication, so check your version.
6Follow-up questions
Can we make JSONObject keep insertion order?
Not through a supported option. Some people subclass it or replace its internal map with reflection to use a LinkedHashMap, but that depends on internals that can change in any release. If order matters, it is a sign that the response should be a typed object serialized by a binding library.
Is it safe to parse money into a double on the client?
No. A double cannot represent most decimal fractions exactly, so 10.10 becomes a nearby binary value and sums drift by fractions of a cent. Clients should parse money into BigDecimal or an integer of minor units, and the server contract should make that easy, for example by sending amounts as strings.
How would you test that the output does not change again?
With approved JSON files and a comparison that matches what the contract promises. JSONAssert in strict mode checks values and fails on extra fields while ignoring key order, which is correct for JSON. If a partner signs the exact text, a plain string comparison against a canonical form is the right test.
7Conclusion
In this answer, we saw that org.json's JSONObject is backed by a HashMap, so key order follows hashing rather than insertion, and that its number formatting has changed between versions and may trim 10.10 to 10.1. Key order should not matter to a correct client, but string comparisons, caches and signatures do depend on it, and a lost scale can be a real bug for money. We replaced hand-built responses with records serialized by Jackson or JSON-B, which fix the property order and keep BigDecimal exact, and kept org.json only for small isolated uses with its version pinned and its licence checked.
Open with
org.json's JSONObject is a thin wrapper around a HashMap, so when it prints itself, the keys come out in hash order, not in the order we called put, and that order can change when keys or library versions change.
Then, if they want more
The number issue comes from org.json's own number formatting: depending on the version, a BigDecimal may be printed through a routine that trims trailing zeros, so 10.10 becomes 10.1, and some versions convert values in other ways, so I would check the version the service uses.
Strictly speaking, JSON objects are unordered, so a client that parses properly should not care about key order. But tests that compare strings, caches that hash the body and especially partners who sign or verify the JSON text all break.
The number change is more often a real bug, for example on a receipt or when a client compares BigDecimal with equals, which includes the scale. My fix would be to stop building responses by hand and define a record for the response, serialized with Jackson, or JSON-B on a Jakarta server.
Records give a fixed property order, and Jackson writes BigDecimal exactly, so 10.10 stays 10.10; for money I might even send the amount as a string to make the contract explicit.
I would capture today's output in tests first so every change is deliberate, and keep org.json only for small isolated uses, with its version pinned and its licence checked against our policy.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Code using org.json builds a response with new JSONObject().put("amount", new...": https://scrivio-interview-library.netlify.app/java/json-libraries/f77e0464a3/