Re: printing JsonbPair values of input JSONB on server side? - Mailing list pgsql-general

From Michel Pelletier
Subject Re: printing JsonbPair values of input JSONB on server side?
Date
Msg-id CACxu=vLCUX8Wq3gkNo+YF_VATNy2MU6pimEXB52yOEAO2MK5Tg@mail.gmail.com
Whole thread Raw
In response to Re: printing JsonbPair values of input JSONB on server side?  (T L <tinlyx@gmail.com>)
Responses Re: printing JsonbPair values of input JSONB on server side?
List pgsql-general
jsonb_each is a wrapper around each_worker_jsonb.  It produces a row for every key/value pair in an object.


the iteration is:

  while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)



On Tue, Mar 19, 2019 at 11:20 AM T L <tinlyx@gmail.com> wrote:
I need this in my C code on the server side. Any link to the `jsonb_each` for this? Examples I found in a quick search are on the client side in SQL.

I am just confused about the various jsonb types and how to effectively extract values and convert between them:

There are Jsonb, JsonbValue (plus the associated JsonbPair ) to begin with. The ` JsonbToCStringWorker ` example that Andrew pointed out uses still another "JsonbContainer" type.
But the type I get from "PG_GETARG_JSONB_P" is Jsonb. And it doesn't fit into " JsonbContainer" or the pointer math about "JsonPair" that I found online.

What I am struggling with adapting some of the iterator code I saw is how to delete irrelevant code without breaking it. My use case is very restricted and handles hstore-like jsonb's.
I don't need or want the code to have the ability to descend into nested objects or handle arrays etc., as they are invalid input in my case.

I thought the pointer math example I found is easier to adapt, but I couldn't get a valid "JsonbPair" from the input parameter to feed into the pointer math.






On Tue, Mar 19, 2019 at 9:50 AM Michel Pelletier <pelletier.michel@gmail.com> wrote:
Yeah I'm not sure why you're looping using pointer math, the iterators are there to provide that service.  Another function to check out 'jsonb_each', other than the set returning function parts, it does what it looks like your are trying to do.

-Michel

On Mon, Mar 18, 2019 at 4:12 PM Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "T" == T L <tinlyx@gmail.com> writes:

 T> Below is my test. It prints a strange character instead of "a"; and
 T> says that the value isn't numeric.

Yeah, there's plenty else wrong with your code.

Did you look at how JsonbToCStringWorker does it? that looks like the
best example I can find on a quick scan.

--
Andrew (irc:RhodiumToad)

pgsql-general by date:

Previous
From: T L
Date:
Subject: Re: printing JsonbPair values of input JSONB on server side?
Next
From: "Peter J. Holzer"
Date:
Subject: Re: Performance of ByteA: ascii vs binary