{"id":314,"date":"2011-12-27T21:43:16","date_gmt":"2011-12-28T02:43:16","guid":{"rendered":"http:\/\/littlesvr.ca\/grumble\/?p=314"},"modified":"2012-12-05T00:48:59","modified_gmt":"2012-12-05T05:48:59","slug":"modifying-json-using-a-form","status":"publish","type":"post","link":"http:\/\/littlesvr.ca\/grumble\/2011\/12\/27\/modifying-json-using-a-form\/","title":{"rendered":"Modifying JSON using a form"},"content":{"rendered":"<p>If you read the <a href=\"http:\/\/littlesvr.ca\/grumble\/2011\/12\/27\/translating-template-files\/\">slightly older post<\/a> and look at its screenshot and do some thinking &#8211; you might like me wonder this: given a bunch of JSON with multiple selections which can be modified in JavaScript using a form.. wait, modified using a form?<\/p>\n<p>One of the nice things about json is you can just do myjson[x].whatever[whatever].12 = &#8220;ABC&#8221; and it works. But if all you have is a &lt;select&gt; element and an onClick handler &#8211; that&#8217;s not so straightforward.<\/p>\n<p>You can store the string &#8220;[x].whatever[whatever].12&#8243; in the value field of the &lt;option&gt; but sadly you cannot just do myjson&#8221;[x].whatever[whatever].12&#8221; = &#8220;DEF&#8221;, that&#8217;s a syntax error.<\/p>\n<p>I had to wonder and look for a while, I even found something called JsonPath, which I got really excited about until I realised it&#8217;s only for reading (what exactly is the point of it then?). Today I found the solution: eval!<\/p>\n<p>So continuing the lame example above I would simply do this: eval(&#8216;myjson[&#8216;+x+&#8217;].&#8217;+whatever+'[&#8216;+whatever&#8217;+&#8217;].&#8217;+12+&#8217; = &#8220;&#8216;+&#8221;DEF&#8221;+'&#8221;&#8216;) Roughly speaking, I haven&#8217;t tested this line. But you get the point?<\/p>\n<p>Now that works but luckily because I&#8217;m testing with a real pot file I found a bug &#8211; all the &#8216;\\n&#8217; literals in the strings are replaced with newlines by eval, which is not what I want. I found a solution that not only fixes that but I hope will prevent my evals from being hacked: I escape all the backslashes and the single quotes in the string before giving it to eval:<\/p>\n<p>eval(jsPath + &#8221; = &#8216;&#8221; + text.toString().replace(\/\\\\\/g,&#8217;\\\\\\\\&#8217;).replace(\/&#8217;\/g,&#8221;\\\\'&#8221;) + &#8220;&#8216;;&#8221;);<\/p>\n<p>Yey!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you read the slightly older post and look at its screenshot and do some thinking &#8211; you might like me wonder this: given a bunch of JSON with multiple selections which can be modified in JavaScript using a form.. wait, modified using a form? One of the nice things about json is you can &hellip; <\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-andrew","4":"post-314","6":"format-standard","7":"category-ostd","8":"category-safeforseneca"},"_links":{"self":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":3,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":581,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/314\/revisions\/581"}],"wp:attachment":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/media?parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/categories?post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/tags?post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}