Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
How to build a nested object in jsonb C function?

I am able to build and return a simple jsonb object, but i'm stuck on trying to use a nested object as a value.

For example, I would like to return the jsonb object:

{"one":1.0,"nest":{"a":1.0,"b":2.0}}

The following code returns the error unknown type of jsonb container specifically on this line:

result.res = pushJsonbValue(&result.parseState, WJB_VALUE, &obj_val);

Here is my current code:

Datum
build_a_nested_object(PG_FUNCTION_ARGS)
{

    Datum num_val;
    JsonbInState result, nested_result;
    JsonbValue key, val, obj_val;

    memset(&result, 0, sizeof(JsonbInState));
    result.res = pushJsonbValue(&result.parseState, WJB_BEGIN_OBJECT, NULL);

    // first key
    key.type = jbvString;
    key.val.string.len = 3;
    key.val.string.val = "one";
    result.res = pushJsonbValue(&result.parseState, WJB_KEY, &key);

    // first value
    val.type = jbvNumeric;
    num_val = DirectFunctionCall3(numeric_in, CStringGetDatum("1.0"), ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1));
    val.val.numeric = DatumGetNumeric(num_val);
    result.res = pushJsonbValue(&result.parseState, WJB_VALUE, &val);

    // Second key
    key.type = jbvString;
    key.val.string.len = 4;
    key.val.string.val = "nest";
    result.res = pushJsonbValue(&result.parseState, WJB_KEY, &key);

    // Second value
    memset(&nested_result, 0, sizeof(JsonbInState));
    nested_result.res = pushJsonbValue(&nested_result.parseState, WJB_BEGIN_OBJECT, NULL);

    // First nested key
    key.type = jbvString;
    key.val.string.len = 1;
    key.val.string.val = "a";
    nested_result.res = pushJsonbValue(&nested_result.parseState, WJB_KEY, &key);

    // First nested value
    val.type = jbvNumeric;
    numd = DirectFunctionCall3(numeric_in, CStringGetDatum("1.0"), ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1));
    val.val.numeric = DatumGetNumeric(numd);
    nested_result.res = pushJsonbValue(&nested_result.parseState, WJB_VALUE, &val);

    // Second nested key
    key.type = jbvString;
    key.val.string.len = 1;
    key.val.string.val = "b";
    nested_result.res = pushJsonbValue(&nested_result.parseState, WJB_KEY, &key);

    // Second nested value
    val.type = jbvNumeric;
    numd = DirectFunctionCall3(numeric_in, CStringGetDatum("2.0"), ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1));
    val.val.numeric = DatumGetNumeric(numd);
    nested_result.res = pushJsonbValue(&nested_result.parseState, WJB_VALUE, &val);

    nested_result.res = pushJsonbValue(&nested_result.parseState, WJB_END_OBJECT, NULL);
    obj_val.type = jbvBinary;
    obj_val.val.binary.data = JsonbValueToJsonb(nested_result.res);

    result.res = pushJsonbValue(&result.parseState, WJB_VALUE, &obj_val);
    result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);

    PG_RETURN_POINTER(JsonbValueToJsonb(result.res));

}
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!