title: how to parse nested key value pairs in R
body: <p>i am trying to parse a log file which contains structure in the form of key value pairs.</p>
  
  <pre><code>log &lt;-  c("name:praveen,age:23,place:UP,address:,dob:, site: {site_name:something , site_url: http://something.com, description:}")  
  </code></pre>
  
  <p>i am trying to parse this line i have done some work but i have two main problem here .</p>
  
  <p>1: How can i  parse "site" variable (shown above) because for  site key there are multiple key:value pair ?  </p>
  
  <p>2: How to tackle with condition if separator comes as a string . like for key:value pair separator is colon (:) and in the "site" key there is a key:value pair <code>site_url:http://something.com</code> here url also contains colon (:) which gives the wrong answer.</p>
  
  <p>this is my code it does not contains "site" key becuase i don't know how to parse it </p>
  
  <pre><code>    log &lt;-  c("name:praveen,age:23,place:UP,address:,dob:")  
      names &lt;- setNames(1:5,c("name","age","place","address","dob"))
  
      assign &lt;- function(x, names){     
        key_value &lt;- sapply(x, function(i)if(length(i)==2L) i else c(i, "nothing"))
        z &lt;- rep(NA, length(names))
        z[names[key_value[1, ]]] &lt;-  key_value[2, ]
        z
      }
  
      split_by_comma &lt;- strsplit(log,",")
      split_by_colon &lt;- lapply(split_by_comma,strsplit,":")    
      ret &lt;- t(sapply(split_by_colon, assign, names))
      colnames(ret) &lt;- names(names)
      ret
  </code></pre>
  
  <p>please help me to parse this file thank you</p>
  
  <p>i have updated with actual log file format.</p>
  
  <pre><code>{
      "username": "lavita",
      "host": "10.105.22.32",
      "event_source": "server",
      "event_type": "/courses/IITB/CS101/2014_T1/xblock/i4x:;_;_IITB;_CS101;_video;_d333fa637a074b41996dc2fd5e675818/handler/xmodule_handler/save_user_state",
      "context": {
          "course_id": "IITB/CS101/2014_T1",
          "course_user_tags": {},
          "user_id": 42,
          "org_id": "IITB"
      },
      "time": "2014-06-20T05:49:10.468638+00:00",
      "ip": "127.0.0.1",
      "event": "{\"POST\": {\"saved_video_position\": [\"00:02:10\"]}, \"GET\": {}}",
      "agent": "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0",
      "page": null
  }
  
  {
      "username": "raeha",
      "host": "10.105.22.32",
      "event_source": "server",
      "event_type": "problem_check",
      "context": {
          "course_id": "IITB/CS101/2014_T1",
          "course_user_tags": {},
          "user_id": 40,
          "org_id": "IITB",
          "module": {
              "display_name": ""
          }
      },
      "time": "2014-06-20T06:43:52.716455+00:00",
      "ip": "127.0.0.1",
      "event": {
          "submission": {
              "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": {
                  "input_type": "choicegroup",
                  "question": "",
                  "response_type": "multiplechoiceresponse",
                  "answer": "MenuInflater.inflate()",
                  "variant": "",
                  "correct": true
              }
          },
          "success": "correct",
          "grade": 1,
          "correct_map": {
              "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": {
                  "hint": "",
                  "hintmode": null,
                  "correctness": "correct",
                  "npoints": null,
                  "msg": "",
                  "queuestate": null
              }
          },
          "state": {
              "student_answers": {},
              "seed": 1,
              "done": null,
              "correct_map": {},
              "input_state": {
                  "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": {}
              }
          },
          "answers": {
              "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": "choice_0"
          },
          "attempts": 1,
          "max_grade": 1,
          "problem_id": "i4x://IITB/CS101/problem/33e4aac93dc84f368c93b1d08fa984fc"
      },
      "agent": "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0",
      "page": "x_module"
  }
  
  
  {
      "username": "tushars",
      "host": "localhost",
      "event_source": "server",
      "event_type": "/courses/IITB/CS101/2014_T1/instructor_dashboard/api/list_instructor_tasks",
      "context": {
          "course_id": "IITB/CS101/2014_T1",
          "course_user_tags": {},
          "user_id": 6,
          "org_id": "IITB"
      },
      "time": "2014-06-20T05:49:26.780244+00:00",
      "ip": "127.0.0.1",
      "event": "{\"POST\": {}, \"GET\": {}}",
      "agent": "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0",
      "page": null
  }
  </code></pre>