DEV Community

Cover image for active is not a CSV column. It stays inside meta
Ameen Usman
Ameen Usman

Posted on

active is not a CSV column. It stays inside meta

I expected active to become its own CSV header. It did not. The JSON converter keeps top-level keys only. A nested object is one cell of JSON text, with inner quotes doubled so the row stays valid CSV. Parsing and conversion stay in the browser. Nothing is uploaded.

What the sample actually prints

This is the Sample button array on the page:

[
  {"id": 1, "name": "Ada", "role": "engineer", "meta": {"active": true}},
  {"id": 2, "name": "Linus", "role": "founder", "meta": {"active": false}}
]
Enter fullscreen mode Exit fullscreen mode

CSV output:

id,name,role,meta
1,Ada,engineer,"{""active"":true}"
2,Linus,founder,"{""active"":false}"
Enter fullscreen mode Exit fullscreen mode

active sits inside meta, so it is not a header. An array value is also one cell. {"id":1,"tags":[]} converts to a tags cell of [].

The same nest is not flat in TypeScript

TypeScript drafts from the first object only. Nested meta becomes a second interface. The root name is always Root.

export interface Root {
  id: number;
  name: string;
  role: string;
  meta: Meta;
}

export interface Meta {
  active: boolean;
}
Enter fullscreen mode Exit fullscreen mode

The PHP DTO does not create a child class. meta is public array $meta. Laravel keeps a nested PHP array. Three formats, three shapes, one paste.

When CSV refuses the paste

[] is valid JSON, but CSV will not convert it. The error is CSV conversion expects an array of objects. and the output is cleared. A trailing comma such as {"a":1,} clears every format with Expected double-quoted property name in JSON at position 7 (line 1 column 8). Repair that in the JSON formatter, then convert again.

CSV is a flat export of top-level keys. It is not a nested schema. Check the draft before you treat a cell as a column.

The sample, the first-object limit, and the other formats are in the guide: https://kepttools.com/guides/json-to-typescript-php-dto-laravel

Top comments (0)