DEV Community

Cover image for [] is valid JSON. CSV still clears the output
Ameen Usman
Ameen Usman

Posted on

[] is valid JSON. CSV still clears the output

I pasted [] into the JSON converter and expected one answer. The four formats disagreed. [] is valid JSON. It is not an empty text box, and it is not a parse error. Conversion stays in the browser. Nothing is uploaded.

What each format prints

Laravel walks the whole value and prints an empty PHP array:

[]
Enter fullscreen mode Exit fullscreen mode

TypeScript drafts from the first object when the root is an array. There is no first object, so the interface is empty Root with a blank line between the braces:

export interface Root {

}
Enter fullscreen mode Exit fullscreen mode

The PHP DTO is still class DataTransferObject. It has declare(strict_types=1), no properties, and an empty constructor:

<?php

declare(strict_types=1);

class DataTransferObject
{


    public function __construct(array $data = [])
    {

    }
}
Enter fullscreen mode Exit fullscreen mode

CSV does not convert []. The error is CSV conversion expects an array of objects. and the output is cleared.

Not the same as a broken paste

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). A lone { stops at column 2. An empty box says Unexpected end of JSON input. Those are parse failures. [] parses. CSV is the format that still refuses it.

Repair broken JSON in the JSON formatter, then convert again. For CSV, paste an array of objects.

Guide: https://kepttools.com/guides/json-to-typescript-php-dto-laravel

Top comments (2)

Collapse
 
prascli profile image
Indra Prasetya •

In our CLI (Argon), -o json emits branches: [] for an empty result set, not empty stdout. Empty stdout is not valid JSON and breaks jq pipelines downstream. Four commands now register -o/--output and emit the array literal: gc, restore --preview, restore --reset, and restore --branch.

Collapse
 
ameenusman profile image
Ameen Usman •

That matches the empty-set case cleanly. [] is still JSON, so jq and anything else that expects a document can keep going. Empty stdout is a different failure mode — the pipeline has nothing to parse.

Keeping -o json on gc and the restore variants as an array literal is the right shape for that path. On the converter side we hit the same tension: Laravel can print [], while CSV still refuses because it wants rows of objects, not an empty list.