Re: remove null values from json

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Michael Moore <michaeljmoore(at)gmail(dot)com>
Cc: postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: remove null values from json
Date: 2016-01-19 18:09:06
Message-ID: CAFj8pRCHZmLKiYpGmdbFYeeqK8PQ9u0acQ6W4vU9TshO2J=4wQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

2016-01-15 21:24 GMT+01:00 Michael Moore <michaeljmoore(at)gmail(dot)com>:

> This is ALMOST what I want:
> select json_strip_nulls ('[{ "f1":1 , "f2":null , "f3":"NULL" , "f4":""
> }]');
> the result is:
> "[{"f1":1,"f3":"NULL","f4":""}]"
>
> As you can see, the "f2" object has been removed. I would *also* like to
> remove the f3 and f4 objects.
>
> I realize I won't be able to use the json_strip_nulls function to do this.
> I could brute force it by doing "json to array" then looping through the
> array and picking only the objects I want, and then using the chosen
> objects to build an new JSON document.
>
> I suspect there is a more elegant way to accomplish the same objective.
> Ideas?
>

if you generate JSON from zero, then replace "" and "NULL" by NULL before
jsonification.

Pavel

>
> PS. It would be great to have a json_strip_values (json, array) where
> array contains the list of values you want removed.
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message David Ford 2016-01-19 19:59:27 filtering columns in function
Previous Message Andreas Joseph Krogh 2016-01-18 17:26:32 Re: BYTEA