Blog

JSON inside a string: how to read an API response like that

The response has a MENU_JSON field, and inside it is a string of quotes and backslashes. You can’t read it by eye, field search doesn’t see it, and nobody will notice when the price type inside changes. Microkoi Bench expands such strings as regular JSON and tracks the fields inside them just like the rest of the response.

Why a backend sends JSON as a string

A restaurant menu is stored in the database as a ready document, and the server puts it into the response as is — as a string. This happens with older services, message queues and settings edited in an admin panel. It’s convenient for the server and for nobody else.

As received
{
  "status": "ok",
  "result": {
    "MENU_JSON": "{\"dishes\":[{\"id\":7,\"price\":390}]}"
  }
}
  • You can’t read the string by eye: quotes are escaped, there are no line breaks.
  • The response model says “string” and keeps quiet about the dishes, prices and IDs inside.
  • If price inside the string becomes a string, no format check will see it.

How to spot such a response in traffic

A gray tilde ~ in the first column of the Traffic table, where the mock and chaos icons go, means the JSON response has a string field holding a whole JSON document. The tooltip names those fields: “JSON inside a string in the response: result.MENU_JSON”, and if the whole response came as a string, “The whole response is JSON inside a string”. Bench checks the response in the background, so the tilde appears a moment after the row and doesn’t slow recording. The column’s other icons are covered in “How to read the traffic table: icons, status codes, response source”.

To keep only such exchanges in the list, open “Filters” and check “JSON in string” in the “Show only” group. An exchange whose tilde appears later joins the filtered list on its own.

Expand the string

  1. Select the exchange and open the response body on the Body tab in the “Format” view.
  2. Above the body, switch from “As received” to “Expand JSON in strings”.
Expand JSON in strings
{
  "status": "ok",
  "result": {
    "MENU_JSON": {
      "dishes": [
        { "id": 7, "price": 390 }
      ]
    }
  }
}

The string is shown as nested, formatted JSON, with a “~ JSON in string” tag next to the field. JSON inside JSON in a string expands too. The choice is remembered for all exchanges and survives a Bench restart.

  • Search with ⌘F in the expanded view finds fields inside the string.
  • Arrays and objects collapse with the arrow left of the field into a single [ … ] or { … } line with the number of items or fields. Search and copy work with the full body.
  • “Copy expanded body” copies regular JSON you can paste into any editor.
  • “Copy as received” copies the response byte for byte, as the server sent it.

Only a string that starts with { or [ and parses fully as JSON counts as JSON in a string. A number, true or text in square brackets like [menu draft] stays a string.

Fields inside the string in the model

In the “Model” view, the MENU_JSON field has the type string (JSON) and expands with an arrow. Under it is a “~ JSON in string” row, and below that, the document’s fields. Their paths go through a tilde:

PathType
result.MENU_JSONstring (JSON)
result.MENU_JSON~dishesarray
result.MENU_JSON~dishes[].pricenumber
order~courier~namestring — second level of nesting

A change inside the string shows up like any other

Fields inside the string are part of the endpoint contract. If price in the menu became a string, it’s a regular mismatch: an icon on the table row, a number in the endpoint tree, the “Contract changed” bar with the path result.MENU_JSON~dishes[].price and number → string. If the backend stops packing the menu into a string and sends an object, that’s a type change string (JSON) → object.

Comparing two exchanges catches it too, and so does a monitor that remembers the model of its response. In the comparison, the path on the “Structure” tab goes through ~, and on the “Body” tab the string is shown expanded with a “~ JSON in string” tag, with the change marked on the line of the field itself, such as price, not the whole long string. In the “As received” view the whole field with a mismatch inside is highlighted, in the expanded view — the field itself.

A contract Bench built before it started expanding such strings rebuilds itself soon after the workspace opens, without waiting for a new response. This adds no contract changes and no marks on exchanges.

Paths with a tilde in checks

A ~ path works everywhere Bench takes a value from a response.

WhereExample
Monitor condition “JSON value”result.MENU_JSON~dishes[0].price greater than 0
Chain step expectationsresult.MENU_JSON~dishes exists
“After response” ruleresult.MENU_JSON~dishes[0].id → dishId

If a key in the response itself contains a tilde, write it as \~ in the path. Without that, ~ means stepping into JSON in a string. A field inside such a string can’t be changed in a chain step’s own values — the string can only be set as a whole.

What you get

A response with a string inside reads like any other: fields are visible, search finds them, changes are flagged. Checks and chain variables take values from deep inside the string without manual parsing.

Open a response with a JSON string in beta 0.9.6 and write to beta@microkoi.com about where else you run into responses like this. We read every email.

Microkoi BenchTraffic analysis and debuggingHTTP and HTTPS, decrypted

Try it on your own project

The beta is free and needs no sign-up. Download it, pick your project folder and start the proxy — your first requests will show up within minutes.

Version 0.9.6 · macOS, Windows and Linux · no sign-up