title: Storing JSON string in Berkeley DB using C
body: <p>Below is my code:</p>
  
  <pre><code>#include &lt;json/json.h&gt;
  #include &lt;stdio.h&gt;
  
  #include &lt;sys/types.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;db.h&gt;
  
  #define  DATABASE "access.db" 
  
  int db_json(json_object  val);
  
  void json_parse(json_object * jobj) {
  
   enum json_type type;
   json_object_object_foreach(jobj, key, val) {
   type = json_object_get_type(val);
   switch (type) {
   case json_type_string: printf("type: json_type_string, ");
   printf("value: %s\n", json_object_get_string(val));
   db_json(val);  
   break;
   }
   }
   }
  
   int db_json(json_object  val) {
  
  typedef struct {
  
  char data1[500];
  
  } pearson_record;
  
  pearson_record s;
  
  
  int i =0;
   DB *dbp;
      DBT key, data;
      int ret, t_ret;
      int recno;
  
  
  
      if ((ret = db_create(&amp;dbp, NULL, 0)) != 0) {
          fprintf(stderr, "db_create: %s\n", db_strerror(ret));
          exit (1);
      }
  
      // if ((ret = dbp-&gt;set_flags(dbp, DB_RECNUM)) != 0) {
          // fprintf(stderr, "db_create: %s\n", db_strerror(ret));
          // exit (1);
      // }
  
      if ((ret = dbp-&gt;open(dbp,
          NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
          dbp-&gt;err(dbp, ret, "%s", DATABASE);
          goto err;
      }
  
      strncpy(s.data1, val, strlen(val)+1);
  
      //printf("chk %\n",jvalue);
  
      recno = 10;
  
      memset(&amp;key, 0, sizeof(key));
      memset(&amp;data, 0, sizeof(data));
      //memset(&amp;s, 0, sizeof(struct pearson_record));
      key.data = &amp;recno;
      key.size = sizeof(recno);
      data.data = &amp;s;
      data.size = sizeof(s);
  
  
       if ((ret = dbp-&gt;put(dbp, NULL, &amp;key,&amp;data,0)) == 0)
      printf("db: %d: key stored.\n", *(int *)key.data);
      else
      {
          dbp-&gt;err(dbp, ret, "DB-&gt;put");
          goto err;
      }
  
      pearson_record *ppr;
           if ((ret = dbp-&gt;get(dbp, NULL, &amp;key, &amp;data, 0)) == 0) {
  
      ppr = (pearson_record *) data.data;
  
      printf("db: %d: key retrieved: data was %s %d\n",
              *(int *)key.data, ppr-&gt;data1, data.size);
      }
      else {
          dbp-&gt;err(dbp, ret, "DB-&gt;get");
          goto err;
      }
  
      err:    if ((t_ret = dbp-&gt;close(dbp, 0)) != 0 &amp;&amp; ret == 0)
          ret = t_ret; 
  
          exit(ret);
      }
  
  int main() {
   char * string = "{\"Extracurricular\" : \"5,6,7,41,40,22,9,12,91,97\"}";
   printf ("JSON string: %s\n", string);
   json_object * jobj = json_tokener_parse(string);
   json_parse(jobj);
  }
  </code></pre>
  
  <p>The error I am getting is:
  ex.c: In function âjson_parseâ:
  ex.c:21:2: error: type of formal parameter 1 is incomplete
  ex.c: At top level:
  ex.c:27:27: error: parameter 1 (âvalâ) has incomplete type</p>
  
  <p>I am new to JSON can anybody tell me where I am going wrong. It is somehow to do with the way I am accessing the JSON string. Thanks in advance.</p>