format json-parse data into html

Q&A's, tips, howto's
Locked
joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

format json-parse data into html

Post by joejoe »

Hi,

I would like to format json data into html.

I have attempted this for a few days, and success looms. :0)

The json-parse return data is this:

Code: Select all

(("result" (("totalResults" 19) ("products" ((("productTitle" "GILDAN EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1HMtjKuuSBuNjSsziq6zq8pXaz/GILDAN-EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt.jpg") ("productUrl" "https://www.aliexpress.com/item/GILDAN-EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt/32919995838.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt men") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1ZiW5OVXXXXbhXFXXq6xXFXXX5/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt-men.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt-men/2048199251.html")) (("productTitle" "GILDAN EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1gNryaifrK1RjSspbq6A4pFXa6/GILDAN-EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt.jpg") ("productUrl" "https://www.aliexpress.com/item/GILDAN-EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt/32961316694.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt men") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1OQTXX9CWBuNjy0Fhq6z6EVXa1/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt-men.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt-men/32708902913.html")) (("productTitle" "Lisp John McCarthy Lisp t shirt top clothing tee for funny male tshirt harajuku streetwear t-shirt men hip hop C2613") ("imageUrl" "https://ae01.alicdn.com/kf/HLB1KsivXOHrK1Rjy0Flq6AsaFXa8/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-t-shirt-top.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-t-shirt-top-clothing-tee-for-funny-male-tshirt-harajuku-streetwear-t/32951457641.html")) (("productTitle" "Lisp John McCarthy Lisp t shirt men hop t-shirt clothing tshirt male streetwear harajuku funny top hip for tee PF2613") ("imageUrl" "https://ae01.alicdn.com/kf/HLB1fw3tKHvpK1RjSZFqq6AXUVXaG/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-t-shirt-men.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-t-shirt-men-hop-t-shirt-clothing-tshirt-male-streetwear-harajuku-funny/32983702961.html")) (("productTitle" "New Short Sleeve Round Collar Mens T Shirts Fashion 2018 A Lisp Symbol - Mens Crewneck T-shirt - 7 Colours") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1KMFuXiCYBuNkSnaVq6AMsVXam/New-Short-Sleeve-Round-Collar-Mens-T-Shirts-Fashion-2018-A-font-b-Lisp-b-font.jpg") ("productUrl" "https://www.aliexpress.com/item/New-Short-Sleeve-Round-Collar-Mens-T-Shirts-Fashion-2018-A-Lisp-Symbol-Mens-Crewneck-T/32851305347.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo T-shirt Top Lycra Cotton Men T shirt New Design High Quality") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1Q2qyOVXXXXa8aFXXq6xXFXXX2/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-T-shirt-Top.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-short-sleeved-T-shirt-men/2048564231.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo T-shirt Top Lycra Cotton Men T shirt New Design High Quality") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1lv2iX1OSBuNjy0Fdq6zDnVXaz/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-T-shirt-Top.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-T-shirt-Top-Lycra-Cotton-Men-T/32708922788.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1ooXHbzuhSKJjSspjq6Ai8VXa9/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton/32834143521.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1UabedvBNTKJjSszbq6yFrFXaf/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton/32833672611.html")) (("productTitle" "Lisp John McCarthy Lisp t shirt harajuku kawaii t-shirt female femme cartoon tops tshirt women tee clothing graphic K2613") ("imageUrl" "https://ae01.alicdn.com/kf/HTB17jx4e9rqK1RjSZK9q6xyypXaE/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-t-shirt-harajuku.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-t-shirt-harajuku-kawaii-t-shirt-female-femme-cartoon-tops-tshirt-women/32934917454.html")) (("productTitle" "Lisp John McCarthy Lisp Hoodies Men Casual Hip Hop Sweatshirt Traksuit Pullover Hoody Tops E4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1sxcOKXGWBuNjy0Fbq6z4sXXah/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-Hoodies-Men-Casual.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-Hoodies-Men-Casual-Hip-Hop-Sweatshirt-Traksuit-Pullover-Hoody-Tops-E4301/32922051065.html")) (("productTitle" "Lisp John McCarthy Lisp Hoodies Men Hooded Sweatshirts Print Quality Pullover Casual Fashion Tracksuit Streetwear X4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1bTA7IHGYBuNjy0Foq6AiBFXai/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-Hoodies-Men-Hooded.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-Hoodies-Men-Hooded-Sweatshirts-Print-Quality-Pullover-Casual-Fashion-Tracksuit-Streetwear-X4301/32905489790.html")) (("productTitle" "Lisp John McCarthy Lisp Hoodies Men Hooded Sweatshirts Print Quality Pullover Casual Fashion Tracksuit Streetwear X4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1M5cvmTmWBKNjSZFBq6xxUFXa9/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-Hoodies-Men-Hooded.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-Hoodies-Men-Hooded-Sweatshirts-Print-Quality-Pullover-Casual-Fashion-Tracksuit-Streetwear-X4301/32910155757.html")) (("productTitle" "Lisp John McCarthy Lisp 3D Hoodie Hoodies Men Women 2019 Long Sleeve Autumn WInter Tops Tees Casual Sweatshirt Sweatshirts P4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1ENJeXELrK1Rjy0Fjq6zYXFXaH/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-3D-Hoodie-Hoodies.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-3D-Hoodie-Hoodies-Men-Women-2019-Long-Sleeve-Autumn-WInter-Tops-Tees/32946570658.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1X.5ampcJL1JjSZFOq6AWlXXao/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-men-Sweatshirts-Thick.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-men-Sweatshirts-Thick-Combed-Cotton/32834402567.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1V9WKXLNNTKJjSspfq6zXIFXa6/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-men-Sweatshirts-Thick.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-men-Sweatshirts-Thick-Combed-Cotton/32833411013.html")) (("productTitle" "Lisp John McCarthy Lisp 3D Print Hoodies women 2018 Black Cosplay Hoody Sweatshirt kpop Hipster Streetwear Tops Pullovers V2613") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1Y_HUXjDuK1Rjy1zjq6zraFXay/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-3D-Print-Hoodies.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-3D-Print-Hoodies-women-2018-Black-Cosplay-Hoody-Sweatshirt-kpop-Hipster-Streetwear/32935074154.html")))))) ("currentPageNum" 0) ("errorCode" 20010000) ("totalPageNum" 0)) 
I would like to translate this to html to display this:

Code: Select all

<a href="https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-men-Sweatshirts-Thick-Combed-Cotton/32833411013.html">EMACS GNU XEMACS LISP programmer red and blue logo men Sweatshirts Thick Combed Cotton</a>
<img src=https://ae01.alicdn.com/kf/HTB1HMtjKuuSBuNjSsziq6zq8pXaz/GILDAN-EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt.jpg">"
Essentially, grabbing the item information from json and transposing it to display html.

I am trying with lookup and dolist but am having trouble getting into the parentheses without having to create a lookup address for each item.

I can get this far:

Code: Select all

(set 'ali (get-url ali-link))
(set 'alie (json-parse ali))
(println (lookup "products" (lookup "result" alie)))
Thank you greatly for any quick pointer or general direction! :9)

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: format json-parse data into html

Post by ralph.ronnquist »

You may want to build around using ref and ref-all, to "pick raisins"; something like this perhaps:

Code: Select all

(define (raisins jsonitem)
    (format "<a href=\"%s\">%s</a>\n"<img src=\"%s\">\n\n"
          (string (if (ref '("productUrl" ?) jsonitem match true) ($it 1)))
          (string (if (ref '("imageTitle" ?) jsonitem match true) ($it 1)))
          (string (if (ref '("imageUrl" ?) jsonitem match true) ($it 1))) ))

(map raisins (if (ref '("producs" ?) jsondata match true) ($it 1) '())
hth

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Re: format json-parse data into html

Post by HPW »

Hello,
From my first thoughts (untested):

Code: Select all

(setq alie '(("result" (("totalResults" 19) ("products" ((("productTitle" "GILDAN EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1HMtjKuuSBuNjSsziq6zq8pXaz/GILDAN-EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt.jpg") ("productUrl" "https://www.aliexpress.com/item/GILDAN-EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt/32919995838.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt men") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1ZiW5OVXXXXbhXFXXq6xXFXXX5/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt-men.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt-men/2048199251.html")) (("productTitle" "GILDAN EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1gNryaifrK1RjSspbq6A4pFXa6/GILDAN-EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt.jpg") ("productUrl" "https://www.aliexpress.com/item/GILDAN-EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt/32961316694.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt men") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1OQTXX9CWBuNjy0Fhq6z6EVXa1/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt-men.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt-men/32708902913.html")) (("productTitle" "Lisp John McCarthy Lisp t shirt top clothing tee for funny male tshirt harajuku streetwear t-shirt men hip hop C2613") ("imageUrl" "https://ae01.alicdn.com/kf/HLB1KsivXOHrK1Rjy0Flq6AsaFXa8/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-t-shirt-top.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-t-shirt-top-clothing-tee-for-funny-male-tshirt-harajuku-streetwear-t/32951457641.html")) (("productTitle" "Lisp John McCarthy Lisp t shirt men hop t-shirt clothing tshirt male streetwear harajuku funny top hip for tee PF2613") ("imageUrl" "https://ae01.alicdn.com/kf/HLB1fw3tKHvpK1RjSZFqq6AXUVXaG/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-t-shirt-men.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-t-shirt-men-hop-t-shirt-clothing-tshirt-male-streetwear-harajuku-funny/32983702961.html")) (("productTitle" "New Short Sleeve Round Collar Mens T Shirts Fashion 2018 A Lisp Symbol - Mens Crewneck T-shirt - 7 Colours") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1KMFuXiCYBuNkSnaVq6AMsVXam/New-Short-Sleeve-Round-Collar-Mens-T-Shirts-Fashion-2018-A-font-b-Lisp-b-font.jpg") ("productUrl" "https://www.aliexpress.com/item/New-Short-Sleeve-Round-Collar-Mens-T-Shirts-Fashion-2018-A-Lisp-Symbol-Mens-Crewneck-T/32851305347.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo T-shirt Top Lycra Cotton Men T shirt New Design High Quality") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1Q2qyOVXXXXa8aFXXq6xXFXXX2/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-T-shirt-Top.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-short-sleeved-T-shirt-men/2048564231.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo T-shirt Top Lycra Cotton Men T shirt New Design High Quality") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1lv2iX1OSBuNjy0Fdq6zDnVXaz/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-T-shirt-Top.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-T-shirt-Top-Lycra-Cotton-Men-T/32708922788.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1ooXHbzuhSKJjSspjq6Ai8VXa9/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton/32834143521.html")) (("productTitle" "EMACS GNU XEMACS LISP programmers wildebeest men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1UabedvBNTKJjSszbq6yFrFXaf/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmers-wildebeest-men-Sweatshirts-Thick-Combed-Cotton/32833672611.html")) (("productTitle" "Lisp John McCarthy Lisp t shirt harajuku kawaii t-shirt female femme cartoon tops tshirt women tee clothing graphic K2613") ("imageUrl" "https://ae01.alicdn.com/kf/HTB17jx4e9rqK1RjSZK9q6xyypXaE/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-t-shirt-harajuku.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-t-shirt-harajuku-kawaii-t-shirt-female-femme-cartoon-tops-tshirt-women/32934917454.html")) (("productTitle" "Lisp John McCarthy Lisp Hoodies Men Casual Hip Hop Sweatshirt Traksuit Pullover Hoody Tops E4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1sxcOKXGWBuNjy0Fbq6z4sXXah/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-Hoodies-Men-Casual.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-Hoodies-Men-Casual-Hip-Hop-Sweatshirt-Traksuit-Pullover-Hoody-Tops-E4301/32922051065.html")) (("productTitle" "Lisp John McCarthy Lisp Hoodies Men Hooded Sweatshirts Print Quality Pullover Casual Fashion Tracksuit Streetwear X4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1bTA7IHGYBuNjy0Foq6AiBFXai/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-Hoodies-Men-Hooded.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-Hoodies-Men-Hooded-Sweatshirts-Print-Quality-Pullover-Casual-Fashion-Tracksuit-Streetwear-X4301/32905489790.html")) (("productTitle" "Lisp John McCarthy Lisp Hoodies Men Hooded Sweatshirts Print Quality Pullover Casual Fashion Tracksuit Streetwear X4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1M5cvmTmWBKNjSZFBq6xxUFXa9/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-Hoodies-Men-Hooded.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-Hoodies-Men-Hooded-Sweatshirts-Print-Quality-Pullover-Casual-Fashion-Tracksuit-Streetwear-X4301/32910155757.html")) (("productTitle" "Lisp John McCarthy Lisp 3D Hoodie Hoodies Men Women 2019 Long Sleeve Autumn WInter Tops Tees Casual Sweatshirt Sweatshirts P4301") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1ENJeXELrK1Rjy0Fjq6zYXFXaH/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-3D-Hoodie-Hoodies.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-3D-Hoodie-Hoodies-Men-Women-2019-Long-Sleeve-Autumn-WInter-Tops-Tees/32946570658.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1X.5ampcJL1JjSZFOq6AWlXXao/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-men-Sweatshirts-Thick.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-men-Sweatshirts-Thick-Combed-Cotton/32834402567.html")) (("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo men Sweatshirts Thick Combed Cotton") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1V9WKXLNNTKJjSspfq6zXIFXa6/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-men-Sweatshirts-Thick.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-men-Sweatshirts-Thick-Combed-Cotton/32833411013.html")) (("productTitle" "Lisp John McCarthy Lisp 3D Print Hoodies women 2018 Black Cosplay Hoody Sweatshirt kpop Hipster Streetwear Tops Pullovers V2613") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1Y_HUXjDuK1Rjy1zjq6zraFXay/-font-b-Lisp-b-font-John-McCarthy-font-b-Lisp-b-font-3D-Print-Hoodies.jpg") ("productUrl" "https://www.aliexpress.com/item/Lisp-John-McCarthy-Lisp-3D-Print-Hoodies-women-2018-Black-Cosplay-Hoody-Sweatshirt-kpop-Hipster-Streetwear/32935074154.html")))))) ("currentPageNum" 0) ("errorCode" 20010000) ("totalPageNum" 0)))

...Code to output formated html header

(dolist (sublist (lookup "products" (lookup "result" alie)))
	(setq productTitle (lookup "productTitle" sublist))
	(setq imageUrl  (lookup "imageUrl" sublist))
	(setq productUrl  (lookup "productUrl" sublist))
	...
	Code to output formated the inner html...
	...
)
...Code to output formated html footer

Edit: Ralph was faster ;-)
Regards
Hans-Peter

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: format json-parse data into html

Post by joejoe »

Thanks to both Ralph and Hans-Peter!

I am trying Ralph's and am getting nil response.

If I am understanding correctly, Ralph intended that I do something like this:

Code: Select all

(set 'ali (get-url ali-link))
(set 'alie (json-parse ali))

(set 'jsondata (lookup "products" (lookup "result" alie)))
 
(define (raisins jsonitem)
    (format "<a href=\"%s\">%s</a>\n"<img src=\"%s\">\n\n"
          (string (if (ref '("productUrl" ?) jsonitem match true) ($it 1)))
          (string (if (ref '("imageTitle" ?) jsonitem match true) ($it 1)))
          (string (if (ref '("imageUrl" ?) jsonitem match true) ($it 1))) ))
          
(println (map raisins (if (ref '("products" ?) jsondata match true) ($it 1) '())))
When I run this in my browser, I get this result:

Code: Select all

()
When I do

Code: Select all

(println (set 'jsondata (lookup "products" (lookup "result" alie))))
to see what I am working with, I get this:

Code: Select all

((("productTitle" "...
Notice the three leading parenthesis.

When I do this:

Code: Select all

(println (set 'jsonitem (jsondata 0)))
I get this:

Code: Select all

(("productTitle" "EMACS GNU XEMACS LISP programmer red and blue logo T-shirt Top Lycra Cotton Men T shirt New Design High Quality") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1Q2qyOVXXXXa8aFXXq6xXFXXX2/EMACS-GNU-XEMACS-font-b-LISP-b-font-programmer-red-and-blue-logo-T-shirt-Top.jpg") ("productUrl" "https://www.aliexpress.com/item/EMACS-GNU-XEMACS-LISP-programmer-red-and-blue-logo-short-sleeved-T-shirt-men/2048564231.html")) 
It seems the extra set of parenthesis that comes with jsondata are making it so the define cannot only get into the first set, which doesn't have the product data sets? I've tried a bunch of tweaks and am coming back to ask if I might be missing something simple.

Thank you both and all again! :0)

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: format json-parse data into html

Post by ralph.ronnquist »

Yes, I kind of thought that jsondata was the whole thing. Since you have

Code: Select all

(set 'jsondata (lookup "products" (lookup "result" alie)))
it'll already have gone into the "products" value part, and therefore my suggested ref of a ("product" ?) pair in jsondata isn't much good. As you noticed :)

If jsondata holds that value part of "products", then you'd get the simpler map term of:

Code: Select all

(map raisins jsondata) 
. And, completed with a println it might look like:

Code: Select all

(println (join (map raisins jsondata) "\n"))
The key in my suggestion is that the term form, with different x and y:

Code: Select all

(if (ref '(x ?) y match true) ($it 1) '())
picks and returns the value part, i.e. the second element, of the first sub list of y starting with x at any depth in a depth-first traversal. It's a quick way to find stuff especially in a json structure, where one often will want to ignore the actual nesting of sub lists and just focus on the details of interest; the raisins in the cake :)

To be clear, the embedding into an if term:

Code: Select all

(if R ($it 1) '())
serves the purpose of avoiding a bad error if the ref term (i.e. the R above) doesn't find anything, and returns nil. In that way the code avoids the attempt of getting the value part of a list that isn't there (although it would be there had the ref succeeded). That embedding also utilizes the $it feature of newlisp, which simply holds the value of the condition term (it's available in if and and terms).

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: format json-parse data into html

Post by joejoe »

Ralph

Gotcha on the x y term form finding they y data if it is there. Also on the error prevention by returning an empty value instead of getting a nil value from the ref statement. It's brilliant and certainly makes using json data more friendly.

I am getting the

Code: Select all

 ERR: data type and format don't match in function format : 
error after integrating your simpler map suggestion.

Code: Select all

(set 'ali (get-url ali-link))
(set 'alie (json-parse ali))
(set 'jsondata (lookup "products" (lookup "result" alie)))
 
(define (raisins jsonitem)
    (format "<a href=\"%s\">%s</a>\n"<img src=\"%s\">\n\n"
          (string (if (ref '("productUrl" ?) jsonitem match true) ($it 1) '()))
          (string (if (ref '("productTitle" ?) jsonitem match true) ($it 1) '()))
          (string (if (ref '("imageUrl" ?) jsonitem match true) ($it 1) '())) ))
 
(println (join (map raisins jsondata) "\n"))

Code: Select all

(println jsondata)
gives this:

Code: Select all

((("productTitle" "GILDAN EMACS GNU XEMACS LISP programmers wildebeest short-sleeved T-shirt") ("imageUrl" "https://ae01.alicdn.com/kf/HTB1HMtjKuuSBuNjSsziq6zq8pXaz/GILDAN-EMACS-GNU-XEMACS-font-b-LISP-b-font-programmers-wildebeest-short-sleeved-T-shirt.jpg") ("productUrl" "https://www.aliexpress.com/item/GILDAN-EMACS-GNU-XEMACS-LISP-programmers-wildebeest-short-sleeved-T-shirt/32919995838.html")) (("productTitle" ...
What I do notice looking at the JSON result in Firefox, is that there are <font><b>LISP</b></font> in the productTitle, which may be confusing the syntax for format?

In the JSON Firefox display, it shows

Code: Select all

productTitle:     "EMACS GNU XEMACS <font><b>LISP</b></font> programmers wildebeest men Sweatshirts Thick Combed Cotton"
It seems these raisins are quite close to being picked. Thank you again for your guidance, Ralph.

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: format json-parse data into html

Post by ralph.ronnquist »

If you read first argument to format very carefully a few times, you will eventually discover the missing back-quote. :)

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: format json-parse data into html

Post by joejoe »

Blingo!

Raisins have been picked Ralph! Woo hoo! :D

What a treasure it is to have such support!

I learned more than I was looking for and am such happier!

Kind thanks for your suggestion and guidance, again.

It's a happy json raisin pickin from the cake kinda day today! :P

Locked