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

Поиск
Список
Период
Сортировка
От Andrew Tipton
Тема Re: Determining the type (array, object, or scalar) of a JSON value
Дата
Msg-id CA+M2pVWt9J=N9-Oy3Hb_bBC+LSOZM6vtS8m-fpV9k9BcJ93sBw@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Determining the type (array, object, or scalar) of a JSON value  (Merlin Moncure <mmoncure@gmail.com>)
Список pgsql-general
On Wed, Jun 12, 2013 at 11:38 PM, Merlin Moncure <mmoncure@gmail.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

В списке pgsql-general по дате отправления:

Предыдущее
От: 高健
Дата:
Сообщение: Why hash join cost calculation need reduction
Следующее
От: Stephen Frost
Дата:
Сообщение: Re: Why hash join cost calculation need reduction