Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda