Re: Determining the type (array, object, or scalar) of a JSON value

From: Andrew Tipton <andrew(at)kiwidrew(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Determining the type (array, object, or scalar) of a JSON value
Date: 2013-06-13 12:28:34
Message-ID: CA+M2pVWt9J=N9-Oy3Hb_bBC+LSOZM6vtS8m-fpV9k9BcJ93sBw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jun 12, 2013 at 11:38 PM, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:

> no argument against json_typeof, but why can you just peek the first
> non-whitespace character? json is famously easy to parse (see here:
> http://www.json.org/)
>
> create or replace function json_typeof(_json json) returns text as
> $$
> select case substring(ltrim($1::text), 1, 1)
> when '[' then 'array'
> when '{' then 'object'
> end;
> $$ language sql immutable;
>
> you could expand this mechanic fairly easy to cover all json types.
> note exception handlers are very heavy for this type of operation.

Thanks for the suggestion -- hadn't thought of just looking at the first
non-whitespace character. It should be trivial to make this into an
efficient C function that could live in jsonfuncs.c as a built-in. I might
do that and submit a patch for 9.4 CF2.

Cheers,
-Andrew

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephen Frost 2013-06-13 12:44:28 Re: Why hash join cost calculation need reduction
Previous Message 高健 2013-06-13 10:16:51 Why hash join cost calculation need reduction