Posted to tcl by Dereckson at Mon Oct 16 10:20:39 GMT 2017view raw

  1. # Converts a key value key value ... list into a JSON object
  2. proc kvlist2json {listToEncode} {
  3. set objectToEncode {}
  4. foreach {k v} $listToEncode {
  5. lappend objectToEncode $k
  6. lappend objectToEncode [::json::write string $v]
  7. }
  8. ::json::write object {*}$objectToEncode
  9. }


Posted by Dereckson at Mon Oct 16 14:17:02 GMT 2017 [text] [code]

As this data structure is actually a dictionary, this code could be simplified though `dict map` use: proc dict2json {dictToEncode} { ::json::write object {*}[dict map {k v} $dictToEncode { set v [::json::write string $v] }] } It should be noted it considers every value are strings, which is not something wanted in most case, where null/true/false/numeric values should be left unquoted.