flashman
← All guides

Turn JSON objects into MATLAB struct assignments

Generate MATLAB struct lines from one JSON object, including dynamic field names, row vectors, and the errors for nested objects and mixed arrays.

2026-10-18 · 8 min read

  • matlab
  • simulink
  • json

Simulink masks and MATLAB callbacks often want assignments such as params.Kp = 1.2. Calibration files are often JSON. Flashman's struct tool accepts one JSON object and prints one assignment per field, in the object's key order. A root array, a root string, or any other non-object fails with Root must be a JSON object.

Format the JSON first. A trailing comma is a parse error, Expected double-quoted property name, and no assignments are produced. The converter does not repair JSON, and it does not drop comments, because standard JSON has none.

Use a dot for identifiers and parentheses otherwise

A key that is a letter or underscore followed by letters, digits, or underscores is an identifier. Kp, maxTorque, and _gain print as params.Kp, params.maxTorque, and params._gain. A key that fails that test is a dynamic field. max-torque prints params.('max-torque') = 10; and 2fast prints params.('2fast') = 3. MATLAB accepts that parentheses form. A mask script that only expects dotted names will not.

The struct variable name uses the same identifier test. A name such as 1bad is replaced with params, so the left-hand side will not echo the illegal name you typed. Read the first assignment after you change the name box.

  • Prefer identifier keys in the JSON when the mask uses dotted names.
  • Keep hyphenated keys only when the MATLAB uses dynamic fields.
  • Check the variable name on the left of every line.
  • Preserve key order from the formatted JSON.

Map each JSON type to the MATLAB literal

Numbers print as MATLAB numbers. 1.2 stays 1.2. A value parsed from 1e21 prints 1e+21. Booleans stay true and false. null and an absent JSON value that arrives as empty become []. That empty array is a value, not a deleted field. Strings use single quotes, and an apostrophe inside the string is doubled: it's fine becomes 'it''s fine'. An empty string becomes ''.

A JSON array of numbers becomes a row vector with spaces: [1, 2, 3] prints [1 2 3]. A two-dimensional numeric array prints with a semicolon between rows: [[1, 2], [3, 4]] prints [1 2; 3 4]. MATLAB will treat the row vector as 1-by-N. If the mask wants a column, the generated line is the wrong shape even though it is valid syntax. Integers that JavaScript cannot store exactly are rounded when the JSON is parsed, before MATLAB syntax exists.

Flatten objects and keep arrays numeric

A nested object throws Nested objects are not supported in struct fields yet. Split it into identifier fields, or build the inner struct in the mask script. An array that contains anything other than numbers, or a rectangular numeric matrix, throws Only numeric vectors/matrices are supported in arrays. [1, "a"] is in that group. The tool does not emit the fields that were valid and then stop. The error replaces the whole output.

Use the MATLAB array tool when the only question is the matrix. [1 2; 3 4] converts back to a JSON matrix. [1 2, 3 4] has no semicolon, so the comma is another column separator and the result is a four-element row. That collapse is easy to miss in a struct line that was hand-edited after generation.

Diff the lines you will paste into the mask

Put the formatted JSON, the generated assignments, and the previous mask script in the review. Diff the assignments. Confirm every dynamic field is intentional, every [] is a real empty array, and every vector has the orientation the block expects.

The struct tool does not run MATLAB and does not apply units. The JSON formatter is the source check. The array tool is the shape check. The diff is the change check. The note in the model review should quote one field name, one assignment, and the shape.

Try these tools