# Javascript Functions for PostgreSQL

DevFeed: [Javascript Functions for PostgreSQL](<https://devfeed.tech/articles/javascript-functions-for-postgresql-41143.md>)

Original publisher: [Read original article](<https://www.craigkerstiens.com/2013/06/25/Javascript-Functions-for-PostgreSQL/>)

Author: Map

Published: 2013-06-25T20:55:56Z

Content type: tutorial

Language: en

Sources: [Craig Kerstiens](<https://devfeed.tech/sources/craig-kerstiens.md>)

Topics: [PostgreSQL](<https://devfeed.tech/topics/postgresql.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [JSON](<https://devfeed.tech/topics/json.md>), [Heroku Postgres](<https://devfeed.tech/topics/heroku-postgres.md>)

Tags: [functions](<https://devfeed.tech/tags/functions.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [json](<https://devfeed.tech/tags/json.md>), [postgres](<https://devfeed.tech/tags/postgres.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>), [sql](<https://devfeed.tech/tags/sql.md>)

## AI overview

A tutorial on using JavaScript functions inside PostgreSQL with PL/v8 and JSON data. It presents example functions for extracting text and numeric values, and introduces a JSON selection function, noting that PostgreSQL 9.3 would add more built-in JSON support.

## Source excerpt

Javascript in Postgres has gotten a good bit of love lately, part of that is from Heroku Postgres recently adding support for Javascript and part from a variety of people championing the power of it such as @leinweber (Embracing the web with JSON and PLV8) and @selenamarie (schema liberation with JSON and PLV8). In a recent conversation it was pointed out that it seems a bit of headache to have to create your own functions, or at least having an initial collection would make it that much more powerful. While many can look forward to PostgreSQL 9.3 which will have a bit more built in support for JSON a few functions can really help make it more useful today. These are courtesy of Will Leinweber. For each of the following functions I'll highlight an example of using it as well. To get an idea of the data its being run on: select * from example; data -------------------------------------------- {"name":"Craig Kerstiens", + "age":27, + "siblings":1, + "numbers":[ + {"type":"work", + "number":"123-456-7890"}, + {"type":"home", + "number":"456-123-7890"}]} (1 row) get_text CREATE OR REPLACE FUNCTION get_text(key text, data json) RETURNS text AS $$ return data[key]; $$ LANGUAGE plv8 IMMUTABLE STRICT; Then using the function: select get_text('name', data) from example; get_text ---------------- Craig Kerstiens (1 row) get_numeric CREATE OR REPLACE FUNCTION get_numeric(key text, data json) RETURNS numeric AS $$ return data[key]; $$ LANGUAGE plv8 IMMUTABLE STRICT; Then using the function: select get_numeric('siblings', data) from example; get_text ---------------- 1 (1 row) json_select create or replace function json_select(selector text, data json) returns json as $$ exports = {}; (function(a){function z(a){return{sel:q(a)[1],match:function(a){return y(this.sel,a)},forEach:function(a,b){return x(this.sel,a,b)}}}function y(a,b){var c=[];x(a,b,function(a){c.push(a)});return c}function x(a,b,c,d,e,f){var g=a[0]===","?a.slice(1):[a],h=[],i=!1,j=0,k=0,l,m;for(j=0;j<g.length;j++){