Skip to content

Fix: API server returning double-encoded JSON string#1935

Open
kl421 wants to merge 1 commit intorobbrad:masterfrom
kl421:patch-1
Open

Fix: API server returning double-encoded JSON string#1935
kl421 wants to merge 1 commit intorobbrad:masterfrom
kl421:patch-1

Conversation

@kl421
Copy link
Copy Markdown

@kl421 kl421 commented Apr 8, 2026

Issue: > When running the API server, CollectData.run() returns a JSON-formatted string rather than a Python dictionary. Because Connexion automatically serializes the return value, it wraps the already-stringified JSON in another layer of quotes and escape characters (e.g., "{\n "bins": [\n..."). This prevents external dashboards and tools from natively parsing the JSON response.

Fix:
Imported the json module and wrapped the return statement in json.loads(). This converts the string back into a dictionary before handing it to Connexion, allowing the API to serve clean, native JSON payloads.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed API response format to return structured JSON data instead of raw strings, improving data accessibility and consistency.

Fix double-encoded JSON output in API server
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

Modified council_data() function to parse raw string output from CollectData.run() using json.loads() and return the resulting JSON object. Adjusted try: block indentation, affecting exception handling structure.

Changes

Cohort / File(s) Summary
JSON Parsing Update
uk_bin_collection_api_server/server.py
Added json import. Modified council_data() to deserialize CollectData.run() output via json.loads(). Changed try block indentation, altering control flow and exception handling scope.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A rabbit hops through JSON trees,
Parsing strings with newfound ease,
The try block shifts—oh what a sight!
Does indentation match the light?
We parse, we catch, we hope it's right! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'Fix: API server returning double-encoded JSON string' accurately describes the main change: addressing the issue where JSON strings are double-encoded when returned by the API.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
uk_bin_collection_api_server/server.py (1)

35-43: ⚠️ Potential issue | 🔴 Critical

Critical indentation error: try: is outside the function scope.

The try: statement at line 35 has no indentation, placing it at module level rather than inside the council_data() function. This causes a Python IndentationError because the except clause at line 40 is indented at 4 spaces (function body level), which doesn't match the try: at column 0.

The JSON parsing logic is correct—CollectData.run() returns a JSON string from output_json(), and json.loads() properly converts it back to a dict for Connexion to serialize.

🐛 Proposed fix for indentation
-try:
+    try:
         CollectData = UKBinCollectionApp()
         CollectData.set_args(args)
         raw_string_output = CollectData.run()
         return json.loads(raw_string_output)
     except Exception as err:
         logging.error(traceback.format_exc())
         logging.info(f"Schema: {err}")
         raise err
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@uk_bin_collection_api_server/server.py` around lines 35 - 43, The try/except
block is at module scope instead of inside the council_data() function;
move/indent the entire try/except (the lines creating UKBinCollectionApp,
calling CollectData.set_args(args), raw_string_output = CollectData.run(),
json.loads(raw_string_output), and the except that logs traceback and err) so
that the try: aligns with the other statements inside council_data(); ensure
references to UKBinCollectionApp, CollectData.set_args, CollectData.run,
json.loads, logging.error(traceback.format_exc()), logging.info(f"Schema:
{err}") and raise err remain unchanged except for indentation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@uk_bin_collection_api_server/server.py`:
- Around line 35-43: The try/except block is at module scope instead of inside
the council_data() function; move/indent the entire try/except (the lines
creating UKBinCollectionApp, calling CollectData.set_args(args),
raw_string_output = CollectData.run(), json.loads(raw_string_output), and the
except that logs traceback and err) so that the try: aligns with the other
statements inside council_data(); ensure references to UKBinCollectionApp,
CollectData.set_args, CollectData.run, json.loads,
logging.error(traceback.format_exc()), logging.info(f"Schema: {err}") and raise
err remain unchanged except for indentation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 90ae2ff5-fb34-456e-8130-a8e4ad1c6cad

📥 Commits

Reviewing files that changed from the base of the PR and between 60bd3cc and 12890e6.

📒 Files selected for processing (1)
  • uk_bin_collection_api_server/server.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant