Title: SyncStack.setJSON skips single item in "items" array · Issue #183 · contentstack/contentstack-java · GitHub
Open Graph Title: SyncStack.setJSON skips single item in "items" array · Issue #183 · contentstack/contentstack-java
X Title: SyncStack.setJSON skips single item in "items" array · Issue #183 · contentstack/contentstack-java
Description: Description: The SyncStack.setJSON method is incorrectly handling scenarios where the "items" key in the provided JSONObject contains a single item. Instead of processing this single item, the current logic within the else block (when it...
Open Graph Description: Description: The SyncStack.setJSON method is incorrectly handling scenarios where the "items" key in the provided JSONObject contains a single item. Instead of processing this single item, the curr...
X Description: Description: The SyncStack.setJSON method is incorrectly handling scenarios where the "items" key in the provided JSONObject contains a single item. Instead of processing this single item...
Opengraph URL: https://github.com/contentstack/contentstack-java/issues/183
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SyncStack.setJSON skips single item in \"items\" array","articleBody":"**Description:**\nThe [**SyncStack.setJSON** method ](https://github.com/contentstack/contentstack-java/blob/master/src/main/java/com/contentstack/sdk/SyncStack.java#L62C5-L86C10) is incorrectly handling scenarios where the \"**items**\" key in the provided JSONObject contains a single item.\nInstead of processing this single item, the current logic within the else block (when itemsObj is not a JSONArray) logs a warning and initializes an empty syncItems list, effectively skipping the data.\n\n**Code Snippet:**\n```java\nprotected synchronized void setJSON(@NotNull JSONObject jsonobject) {\n if (jsonobject == null) {\n throw new IllegalArgumentException(\"JSON object cannot be null.\");\n }\n\n this.receiveJson = jsonobject;\n\n if (receiveJson.has(\"items\")) {\n Object itemsObj = receiveJson.opt(\"items\");\n if (itemsObj instanceof JSONArray) {\n JSONArray jsonArray = (JSONArray) itemsObj;\n syncItems = new ArrayList\u003c\u003e();\n for (int i = 0; i \u003c jsonArray.length(); i++) {\n JSONObject jsonItem = jsonArray.optJSONObject(i);\n if (jsonItem != null) {\n syncItems.add(sanitizeJson(jsonItem));\n }\n }\n } else {\n logger.warning(\"'items' is not a valid list. Skipping processing.\"); // ✅ Prevent crashes\n syncItems = new ArrayList\u003c\u003e();\n }\n } else {\n syncItems = new ArrayList\u003c\u003e();\n }\n}\n```\n\n**Observed Behavior:**\n\nWhen the \"items\" key in the receiveJson JSONObject contains a single JSON object (not a JSONArray of one element), the code enters the else block and skips processing this item. The syncItems list remains empty in this case.\n\n**Expected Behavior:**\n\nWhen the \"items\" key contains only a single JSON object, it should be treated as a single item to be processed and added to the syncItems list.\n\n**Suggested Solution:**\n\nThe else block should be modified to handle the case where itemsObj is a single JSONObject. A possible solution would be to check if itemsObj is an instance of JSONObject and, if so, create a new ArrayList, add the sanitized itemsObj to it, and assign it to syncItems.\n\n```java\n } else {\n if (itemsObj instanceof JSONObject) {\n syncItems = new ArrayList\u003c\u003e();\n syncItems.add(sanitizeJson((JSONObject) itemsObj));\n } else {\n logger.warning(\"'items' is not a valid list. Skipping processing.\"); // ✅ Prevent crashes\n syncItems = new ArrayList\u003c\u003e();\n }\n }\n```\n\n**Impact:**\n\nThis issue leads to data loss or incomplete synchronization when the \"items\" payload from Contentstack contains a single entry. As there is any exception thrown, we consider the execution as completed, and we are updating the sync token, which leads to data loss.\n\n","author":{"url":"https://github.com/victor-pariente-edo","@type":"Person","name":"victor-pariente-edo"},"datePublished":"2025-04-21T15:09:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/183/contentstack-java/issues/183"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:602fb62a-2002-083a-af79-4ea94191d9c0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E26:CD22B:148DE18:19ED7E2:697F2740 |
| html-safe-nonce | 8a89c232af46e3edf1387dfb62e3b95d58f00e88218a2d18b65a5d98ce665fb5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTI2OkNEMjJCOjE0OERFMTg6MTlFRDdFMjo2OTdGMjc0MCIsInZpc2l0b3JfaWQiOiIyMjc1MjQ5MDY5ODc1OTk2NDgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f14fd1334af689a4972c179d630010444a0d67c73265cb5c4c887def49db7566 |
| hovercard-subject-tag | issue:3008636460 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/contentstack/contentstack-java/183/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c5c6c1022e6d266804e9d047c26ffbb2ab056804c2543f09511f0a950cb16ffa/contentstack/contentstack-java/issues/183 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c5c6c1022e6d266804e9d047c26ffbb2ab056804c2543f09511f0a950cb16ffa/contentstack/contentstack-java/issues/183 |
| og:image:alt | Description: The SyncStack.setJSON method is incorrectly handling scenarios where the "items" key in the provided JSONObject contains a single item. Instead of processing this single item, the curr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | victor-pariente-edo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 60279d4097367e16897439d16d6bbe4180663db828c666eeed2656988ffe59f6 |
| turbo-cache-control | no-preview |
| go-import | github.com/contentstack/contentstack-java git https://github.com/contentstack/contentstack-java.git |
| octolytics-dimension-user_id | 24450751 |
| octolytics-dimension-user_login | contentstack |
| octolytics-dimension-repository_id | 106927781 |
| octolytics-dimension-repository_nwo | contentstack/contentstack-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 106927781 |
| octolytics-dimension-repository_network_root_nwo | contentstack/contentstack-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7c85641c598ad130c74f7bcc27f58575cac69551 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width