<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Parse JSON with AutoLisp / VisualLisp in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9762262#M78151</link>
    <description>&lt;P&gt;Never got around to making a better one. Here's an updated take w/ VisualLISP (see attached).&lt;/P&gt;&lt;P&gt;This version is better than the... &lt;EM&gt;(read (vl-string-translate "[]{}:," "()() " json))&lt;/EM&gt; ...method because the translate method will replace ALL of the input characters (even in json object string), where this new function will not.&lt;/P&gt;&lt;P&gt;A small sample:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="lisp"&gt;(ParseJSON-&amp;gt;List "{\"myTag\" : \"MyTagValue\" , \"myNum\" : -123.4}")
...returns...
(("myTag" "MyTagValue") ("myNum" -123.4))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Sep 2020 22:06:14 GMT</pubDate>
    <dc:creator>CodeDing</dc:creator>
    <dc:date>2020-09-22T22:06:14Z</dc:date>
    <item>
      <title>Parse JSON with AutoLisp / VisualLisp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9332756#M78149</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I have come up with an acceptable parser for JSON -&amp;gt; Lists. Hopefully.&lt;/P&gt;&lt;P&gt;But let's be honest, it'll probably break eventually haha..&lt;/P&gt;&lt;P&gt;With my preliminary testing on the 3 attached files (i have tested more, only attached 3) it appears to be working normally and as-expected. But I'm hoping I can get feedback / improvements!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This initial version is actually entirely in 'vanilla' Autolisp (no VisualLisp), so any future releases I will just dictate with an identifier at the top (al = AutoLisp / vl = VisualLisp).&lt;/P&gt;&lt;P&gt;Ideally, the final function should NOT include a 'read' function as this one currently does. But for now, this is a step in the right direction I believe.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's definitely pretty slow at the moment, but it can handle its own.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The main reason I looked into this was after realizing that the (read) function cannot handle 'symbols' over ~2000 characters (so, one long continuous string [as found in the attached 'Example JSON Google API'] will cause my original (much easier) function to fail:&lt;/P&gt;&lt;PRE&gt;(defun JSON-&amp;gt;LIST (json / tmp dbl nwl)
;json - string, as json data
;returns - list or nil, list of data converted from json
(if (eq 'STR (type json)) (read (vl-string-translate "[]{}:," "()()  " json)) nil)
);defun&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#333333"&gt;Here's the function..&amp;nbsp;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT color="#333333"&gt;Let me know what you think:&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;(defun ParseJSON-&amp;gt;List (json / cnt len prevChar ch endPos tmp ret)
&lt;FONT color="#FF9900"&gt;;Coded by Denon Deterding (Version al:1.0 - 20200221)
;json - string, of json data to convert
;returns - list, of converted json
;helper function(s)&lt;/FONT&gt;
  (defun EndOfQuote (str pos / ch cnt prevChar found)
    (setq cnt 0 prevChar "" found nil)
    (while (not found)
      (setq ch (substr str (+ pos (setq cnt (1+ cnt))) 1))
      (if (and (eq "\"" ch) (not (eq "\\" prevChar))) (setq found t pos (+ cnt pos)))
      (setq prevChar ch)
    );while
    pos
  );defun
  (defun EndOfBracket (str pos openChar / ch cnt prevChar found inStr closeChar)
    (setq cnt -1 prevChar "" found nil inStr nil closeChar (if (eq "{" openChar) "}" "]"))
    (setq openCnt 0 closeCnt 0)
    (while (not found)
      (setq ch (substr str (+ pos (setq cnt (1+ cnt))) 1))
      (if (and (eq "\"" ch) (not (eq "\\" prevChar))) (setq inStr (not inStr)))
      (cond
	((and (not inStr) (eq openChar ch)) (setq openCnt (1+ openCnt)))
	((and (not inStr) (eq closeChar ch)) (setq closeCnt (1+ closeCnt)))
      );cond
      (if (= openCnt closeCnt) (setq found t pos (+ pos cnt)))
      (setq prevChar ch)
    );while
    pos
  );defun
&lt;FONT color="#FF9900"&gt;;main work&lt;/FONT&gt;
(setq cnt 0 str "" ret '())
(repeat (setq len (strlen json))
  (setq ch (substr json (setq cnt (1+ cnt)) 1))
  (cond
    ((or (eq "{" ch) (eq "[" ch))
      (setq endPos (EndOfBracket json cnt ch)
	    cnt (1+ cnt)
	    ret (cons (ParseJSON (substr json cnt (- endPos cnt))) ret)
	    str ""
	    cnt endPos)
    );cond 1
    ((eq "\"" ch)
      (setq endPos (EndOfQuote json cnt)
            cnt (1+ cnt)
	    ret (cons (substr json cnt (- endPos cnt)) ret)
	    str ""
	    cnt endPos)
    );cond 2
    ((or (eq ":" ch) (eq "," ch))
      (setq tmp (read (strcase str)))
      (cond
	((or (eq 'NULL tmp) (eq 'FALSE tmp)) (setq ret (cons nil ret)))
	((eq 'TRUE tmp) (setq ret (cons t ret)))
	((numberp tmp) (setq ret (cons tmp ret)))
	((not (null tmp)) (setq ret (cons (read str) ret)))
      );cond
      (setq str "")
    );cond 3
    ((not (member ch '("\n" "\t" "\b" "\r")))
      (if (or (eq " " ch) (= (1+ len) cnt))
	(progn
	  (setq tmp (read (strcase str)))
	  (cond
	    ((or (eq 'NULL tmp) (eq 'FALSE tmp)) (setq ret (cons nil ret)))
	    ((eq 'TRUE tmp) (setq ret (cons t ret)))
	    ((numberp tmp) (setq ret (cons tmp ret)))
	    ((not (null tmp)) (setq ret (cons (read str) ret)))
	  );cond
	  (setq str "")
	);progn
      ;else
        (setq str (strcat str ch))
      );if
    );cond 4
  );cond
);repeat
(reverse ret)
);defun&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#333333"&gt;&lt;STRONG&gt;PLEASE NOTE:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#333333"&gt;- The one weird &lt;U&gt;caveat&lt;/U&gt;&amp;nbsp;to this function, due to its recursive nature and the way it returns items, is that an extra (list ..) is created around the original data. THIS MEANS that you should ALWAYS surround this function with a (car ..) function to return the correct format. (see below)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#333333"&gt;An Example call:&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;(setq data (car (ParseJSON-&amp;gt;List jsonString)))&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#333333"&gt;If you were to test an example file:&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;(defun c:TEST ( / str f txt data)
(setq str "" f (open "c:\\myFolder\\my sub folder\\example json.txt" "r"))
(while (setq txt (read-line f))
  (setq str (strcat str txt "\n"))
);while
(close f)
(if (setq data (car (ParseJSON-&amp;gt;List str)))
  (princ data)
);if
(princ)
);defun&lt;/PRE&gt;&lt;P&gt;&lt;FONT color="#333333"&gt;Best,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#333333"&gt;~DD&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Feb 2020 08:13:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9332756#M78149</guid>
      <dc:creator>CodeDing</dc:creator>
      <dc:date>2020-02-21T08:13:48Z</dc:date>
    </item>
    <item>
      <title>Re: Parse JSON with AutoLisp / VisualLisp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9333309#M78150</link>
      <description>&lt;P&gt;--UPDATE--&lt;/P&gt;&lt;P&gt;This is what I get for changing the name at the last minute!&lt;/P&gt;&lt;P&gt;This line will need to be updated to reflect the correct function name:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;	    ret (cons (&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ParseJSON&lt;/STRONG&gt;&lt;/FONT&gt; (substr json cnt (- endPos cnt))) ret)
...to...
	    ret (cons (&lt;FONT color="#000000"&gt;ParseJSON-&amp;gt;List&lt;/FONT&gt; (substr json cnt (- endPos cnt))) ret)&lt;/PRE&gt;&lt;P&gt;See attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Feb 2020 12:59:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9333309#M78150</guid>
      <dc:creator>CodeDing</dc:creator>
      <dc:date>2020-02-21T12:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: Parse JSON with AutoLisp / VisualLisp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9762262#M78151</link>
      <description>&lt;P&gt;Never got around to making a better one. Here's an updated take w/ VisualLISP (see attached).&lt;/P&gt;&lt;P&gt;This version is better than the... &lt;EM&gt;(read (vl-string-translate "[]{}:," "()() " json))&lt;/EM&gt; ...method because the translate method will replace ALL of the input characters (even in json object string), where this new function will not.&lt;/P&gt;&lt;P&gt;A small sample:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="lisp"&gt;(ParseJSON-&amp;gt;List "{\"myTag\" : \"MyTagValue\" , \"myNum\" : -123.4}")
...returns...
(("myTag" "MyTagValue") ("myNum" -123.4))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Sep 2020 22:06:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9762262#M78151</guid>
      <dc:creator>CodeDing</dc:creator>
      <dc:date>2020-09-22T22:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Parse JSON with AutoLisp / VisualLisp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9766280#M78152</link>
      <description>&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;**NEW VERSION**&lt;/STRONG&gt;&lt;/U&gt;&lt;BR /&gt;I have realized that the last update I posted (VL1) has a design flaw. When presented with an array as an initial argument, then the list created would close too early, so in the simple example below only the first set of items ("abc" &amp;amp; "def") would be returned and the ("xyz" &amp;amp; "wvu") items would not be. I have attached an updated version (VL2) which is working as expected.&lt;/P&gt;&lt;PRE&gt;[
      {
         "abc" : 123,
         "def" : 456
      },
      {
         "xyz" : 987,
         "wvu" : 654
      }
]&lt;/PRE&gt;&lt;P&gt;Best,&lt;BR /&gt;~DD&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 15:41:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9766280#M78152</guid>
      <dc:creator>CodeDing</dc:creator>
      <dc:date>2020-09-24T15:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: Parse JSON with AutoLisp / VisualLisp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9884193#M78153</link>
      <description>&lt;P&gt;&lt;FONT size="4"&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5680873"&gt;@CodeDing&lt;/a&gt;&amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="4"&gt;I was looking for a solution and bumped into your post.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;Its function works very well !! excellent for the initiative.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;but you have already dealt with a situation like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;my .JSON has a great length I get it from the google API and it takes a long time to read everything and convert it into a list.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="4"&gt;how do you deal to make this processing faster ??&amp;nbsp;since I just need the addresses&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Frjuniornogueira_0-1605895548394.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/846821i12EBC1DB9E1052F0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Frjuniornogueira_0-1605895548394.png" alt="Frjuniornogueira_0-1605895548394.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I attached .json&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 18:08:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9884193#M78153</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-11-20T18:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: Parse JSON with AutoLisp / VisualLisp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9884398#M78154</link>
      <description>&lt;P&gt;@Anonymous&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sadly, I do not believe there is a 'faster' approach using the AutoLISP / Visual LISP method and the Google API.&lt;/P&gt;&lt;P&gt;You will have to Get the web text, convert the json, then search for your data with lisp functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively,&lt;/P&gt;&lt;P&gt;Around the same time of this original post, I reached out to the .NET community to ask if somebody could create an AutoLISP function that could convert the JSON faster (since they have more tools and libraries for such a task).&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp; was kind enough to help me and created a very useful (json2lisp ...) function (via a C# .dll file).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My post on the forums:&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/net/use-net-to-convert-json-via-autolisp/m-p/9382709#M65121" target="_blank"&gt;https://forums.autodesk.com/t5/net/use-net-to-convert-json-via-autolisp/m-p/9382709#M65121&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;His updated function at The Swamp:&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.theswamp.org/index.php?topic=55858.0" target="_blank"&gt;https://www.theswamp.org/index.php?topic=55858.0&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...the only problem that I had with his function was that it ultimately cannot accept a json array as the initial argument. So, in my free time, I have been creating my own .dll file (as well as learning the C# language) with an updated function, and some other useful ones.. such as: an updated web GET call, a GET call that saves the returned response to a file location (such as images, files, etc), functions for custom dwg properties, and the updated json to lisp parser. I plan to add many more functions and document their uses, I was going to release that file in a new post on the forums one day in the future, but If you are interested, I could give you what I have currently and post what functions are available?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;~DD&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 20:01:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9884398#M78154</guid>
      <dc:creator>CodeDing</dc:creator>
      <dc:date>2020-11-20T20:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Parse JSON with AutoLisp / VisualLisp</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9884639#M78155</link>
      <description>&lt;P&gt;&lt;FONT size="4"&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5680873"&gt;@CodeDing&lt;/a&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4"&gt;I will test the .dll on the link&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="4"&gt;I would be very grateful if you could attach or send me messages for me to check.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="4"&gt;already grateful&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 22:04:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/parse-json-with-autolisp-visuallisp/m-p/9884639#M78155</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-11-20T22:04:17Z</dc:date>
    </item>
  </channel>
</rss>

