Bogey
Bogey

Reputation: 5744

read_json_auto in DuckDb without involving files

I'm looking for a way to build up a DuckDB table akin to read_json_auto with the following constraints:

  1. Must work in-memory only. I want to avoid having to load a file from FS
  2. Must be cross-plattform compatible

Is there a way to do so?


Example: Say you have a JSON array such as

[
  {"a": 1, "b": 2},
  {"a": 10, "b": 20}
]

read_json_auto would now automatically infer the columns, and create a table with 2 rows, and 2 columns (column "a" and "b").

Given my constraints above, I thought maybe I can build up a table manually, and then somehow expand its format;

CREATE TABLE Source(items JSON);
INSERT INTO Source VALUES ('{"a": 1, "b": 2}');
INSERT INTO Source VALUES ('{"a": 10, "b": 20}');
// now what?

I haven't found any way to do the next step though - which is, having some convenient way of expanding this table into a table with 2 columns "a" and "b", similar to what read_json_auto would do.

Upvotes: 3

Views: 977

Answers (1)

AlexMonahan
AlexMonahan

Reputation: 86

One option would be to use the CLI and read/write stdin/stdout like in this example: https://duckdb.org/docs/api/cli#reading-from-stdin-and-writing-to-stdout

Upvotes: 0

Related Questions