usebruno/bruno · error · TypeError

minifyJson expects a string or object

Error message

minifyJson expects a string or object

What it means

Thrown as a TypeError by bru.utils.minifyJson when the input is neither a string nor an object — i.e., it is a number, boolean, symbol, or function. The function only accepts null/undefined (which throw 'Failed to minify'), objects, and strings; all other types hit the final TypeError at bru.js:122.

Source

Thrown at packages/bruno-js/src/bru.js:122

        if (typeof json === 'object') {
          try {
            return JSON.stringify(json);
          } catch (err) {
            throw new Error(`Failed to minify: ${err?.message || err}`);
          }
        }

        if (typeof json === 'string') {
          const trimmed = json.trim();
          if (trimmed === '') return trimmed;
          try {
            return JSON.stringify(JSON.parse(trimmed));
          } catch (err) {
            throw new Error(`Failed to minify: ${err?.message || err}`);
          }
        }

        throw new TypeError('minifyJson expects a string or object');
      },

      minifyXml: (xml) => {
        if (xml === null || xml === undefined) {
          throw new Error('Failed to minify');
        }

        if (typeof xml === 'string') {
          try {
            return xmlFormat(xml, { collapseContent: false, indentation: '', lineSeparator: '' });
          } catch (err) {
            throw new Error(`Failed to minify: ${err?.message || err}`);
          }
        }

        throw new TypeError('minifyXml expects a string');
      }
    };

View on GitHub (pinned to 9bdd81c7bd)

Solutions

  1. Check the type of the input before calling minifyJson and convert it to an object or string first.
  2. If you have a primitive, wrap it: minifyJson({ value: primitive }) or minifyJson(JSON.stringify(primitive)).
  3. Ensure the variable or expression you are passing actually contains JSON data.

Example fix

// before
const min = bru.utils.minifyJson(bru.getVar('itemCount')); // returns a number

// after
const val = bru.getVar('itemCount');
const min = (typeof val === 'object' || typeof val === 'string')
  ? bru.utils.minifyJson(val)
  : bru.utils.minifyJson({ value: val });
Defensive patterns

Strategy: type-guard

Validate before calling

function isMinifiableInput(val) {
  return val !== null && val !== undefined && (typeof val === 'object' || typeof val === 'string');
}
// before calling: if (isMinifiableInput(val)) bru.utils.minifyJson(val);

Type guard

function isMinifiableJsonInput(val) {
  return val !== null && val !== undefined && (typeof val === 'object' || typeof val === 'string');
}

Prevention

When it happens

Trigger: Calling bru.utils.minifyJson(42), bru.utils.minifyJson(true), bru.utils.minifyJson(Symbol('x')), or bru.utils.minifyJson(() => {}). Because typeof for these returns 'number', 'boolean', 'symbol', or 'function', none of the preceding branches match.

Common situations: Passing a numeric value from a variable that was expected to be a JSON string or object (e.g., minifyJson(bru.getVar('count')) where count holds a number). Passing a boolean flag by mistake.

Related errors


AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13). Data as JSON: /api/errors/0d7c4aa09d372483. Report an issue: GitHub.