JW_OBJ_KV

Table of Contents

JW_OBJ_KV

Description

Write a key and nested object inside an existing JSON object. Should be called inside JW_OBJ. Adds commas appropriately based on insertion order.

Parameters

NameDirectionDescription
jin,outThe target string to append to.
kinThe key name to use in the JSON object.
writerinA block of code using JW_*_KV to populate the inner object.

Usage example (from documentation)

  JW_OBJ_KV(json, "config", {
      JW_INT_KV(json, "timeout", 30);
  });

Success

Appends a nested JSON object under the given key

Failure

Does not return on failure

Usage example (Cross-references)

    
    JW_OBJ(json, {
    JW_OBJ_KV(json, "user", {
    JW_STR_KV(json, "name", data.user.name);
    JW_STR_KV(json, "email", data.user.email);
    
    JW_OBJ(json, {
    JW_OBJ_KV(
    json,
    "outer",
    );
    JW_ARR_KV(json, "list", empty_list, item, { JW_INT(json, item); });
    JW_OBJ_KV(json, "deep", {
    JW_OBJ_KV(
    json,
    JW_ARR_KV(json, "list", empty_list, item, { JW_INT(json, item); });
    JW_OBJ_KV(json, "deep", {
    JW_OBJ_KV(
    json,
    "inner",
    
    JW_OBJ(json, {
    JW_OBJ_KV(
    json,
    "empty_obj",
    }
    );
    JW_OBJ_KV(json, "filled_obj", { JW_INT_KV(json, "x", x_value); });
    JW_ARR_KV(json, "empty_arr", empty_arr, item, { JW_INT(json, item); });
    JW_ARR_KV(json, "filled_arr", filled_arr, item, { JW_INT(json, item); });
    
    JW_OBJ(json, {
    JW_OBJ_KV(json, "user", {
    JW_INT_KV(json, "id", data.user.id);
    JW_OBJ_KV(json, "profile", {
    JW_OBJ_KV(json, "user", {
    JW_INT_KV(json, "id", data.user.id);
    JW_OBJ_KV(json, "profile", {
    JW_STR_KV(json, "name", data.user.profile.name);
    JW_INT_KV(json, "age", data.user.profile.age);
    
    JW_OBJ(json, {
    JW_OBJ_KV(json, "company", {
    JW_OBJ_KV(json, "departments", {
    JW_OBJ_KV(json, "engineering", {
    JW_OBJ(json, {
    JW_OBJ_KV(json, "company", {
    JW_OBJ_KV(json, "departments", {
    JW_OBJ_KV(json, "engineering", {
    JW_STR_KV(json, "head", data.company.departments.engineering.head);
    JW_OBJ_KV(json, "company", {
    JW_OBJ_KV(json, "departments", {
    JW_OBJ_KV(json, "engineering", {
    JW_STR_KV(json, "head", data.company.departments.engineering.head);
    JW_INT_KV(json, "count", data.company.departments.engineering.count);
    JW_BOOL_KV(json, "status", response.status);
    JW_STR_KV(json, "message", response.message);
    JW_OBJ_KV(json, "data", {
    // Write dynamic key for source function ID
    Str source_key = StrInit();
    StrWriteFmt(&source_key, "{}", source_id);
    
    JW_OBJ_KV(json, source_key.data, {
    if (response.data.length > 0) {
    AnnSymbol* s          = &VecAt(&response.data, 0);
    StrWriteFmt(&target_key, "{}", s->target_function_id);
    
    JW_OBJ_KV(json, target_key.data, {
    JW_FLT_KV(json, "distance", s->distance);
    JW_INT_KV(json, "nearest_neighbor_analysis_id", s->analysis_id);
    
    JW_OBJ(json, {
    JW_OBJ_KV(json, "functions", {
    VecForeach(&symbols, symbol, {
    Str source_key = StrInit();
    StrWriteFmt(&source_key, "{}", symbol.source_function_id);
    
    JW_OBJ_KV(json, source_key.data, {
    Str target_key = StrInit();
    StrWriteFmt(&target_key, "{}", symbol.target_function_id);
    StrWriteFmt(&target_key, "{}", symbol.target_function_id);
    
    JW_OBJ_KV(json, target_key.data, {
    JW_FLT_KV(json, "distance", symbol.distance);
    JW_STR_KV(json, "name", symbol.function_name);
    
    JW_OBJ(json, {
    JW_OBJ_KV(json, "level1", {
    JW_OBJ_KV(json, "level2", {
    JW_OBJ_KV(json, "level3", {
    JW_OBJ(json, {
    JW_OBJ_KV(json, "level1", {
    JW_OBJ_KV(json, "level2", {
    JW_OBJ_KV(json, "level3", {
    JW_STR_KV(json, "message", deep_message);
    JW_OBJ_KV(json, "level1", {
    JW_OBJ_KV(json, "level2", {
    JW_OBJ_KV(json, "level3", {
    JW_STR_KV(json, "message", deep_message);
    JW_INT_KV(json, "value", 42);
    Str json = StrInit();
    JW_OBJ(json, {
    JW_OBJ_KV(json, "user", {
    JW_INT_KV(json, "id", original_person.id);
    JW_STR_KV(json, "name", original_person.name);
    Str json = StrInit();
    JW_OBJ(json, {
    JW_OBJ_KV(json, "user", {
    JW_INT_KV(json, "id", original.user.id);
    JW_STR_KV(json, "name", original.user.name);
    JW_FLT_KV(json, "salary", original.user.salary);
    });
    JW_OBJ_KV(json, "config", {
    JW_BOOL_KV(json, "debug_mode", original.config.debug_mode);
    JW_INT_KV(json, "timeout", original.config.timeout);
    JW_ARR_KV(json, "empty_numbers", empty_numbers, num, { JW_INT(json, num); });
    JW_ARR_KV(json, "empty_strings", empty_strings, str, { JW_STR(json, str); });
    JW_OBJ_KV(
    json,
    "empty_object",

Share :

Related Posts

JR_OBJ

JR_OBJ Description Read a JSON object using a custom field reader expression. The macro parses the object and invokes the provided code block for each key-value pair. If the key is not recognized or parsing fails, the value is skipped.

Read More

JR_FLT_KV

JR_FLT_KV Description Read a float key-value pair if key matches.

Read More

JR_INT_KV

JR_INT_KV Description Read an integer key-value pair if key matches.

Read More