{"id":55,"date":"2016-09-21T19:22:42","date_gmt":"2016-09-21T19:22:42","guid":{"rendered":"https:\/\/blog.diggernaut.com\/?p=55"},"modified":"2019-01-12T21:23:10","modified_gmt":"2019-01-12T21:23:10","slug":"what-to-do-when-server-respond-with-json","status":"publish","type":"post","link":"https:\/\/www.diggernaut.com\/blog\/what-to-do-when-server-respond-with-json\/","title":{"rendered":"What to do when server respond with JSON?"},"content":{"rendered":"<p><a href=\"https:\/\/www.diggernaut.com\">Diggernaut<\/a> makes it easy to work with JSON format by converting it to XML. So now I\u2019ll show you how you can work with it.<\/p>\n<p>I found one of the public JSON Rest API we use as a sample. <a href=\"http:\/\/jsonplaceholder.typicode.com\/users\">Link<\/a>.<\/p>\n<p>It\u2019s a sample of their server response:<\/p>\n<pre><code class=\"language-js\">[\n  {\n    &quot;id&quot;: 1,\n    &quot;name&quot;: &quot;Leanne Graham&quot;,\n    &quot;username&quot;: &quot;Bret&quot;,\n    &quot;email&quot;: &quot;Sincere@april.biz&quot;,\n    &quot;address&quot;: {\n      &quot;street&quot;: &quot;Kulas Light&quot;,\n      &quot;suite&quot;: &quot;Apt. 556&quot;,\n      &quot;city&quot;: &quot;Gwenborough&quot;,\n      &quot;zipcode&quot;: &quot;92998-3874&quot;,\n      &quot;geo&quot;: {\n        &quot;lat&quot;: &quot;-37.3159&quot;,\n        &quot;lng&quot;: &quot;81.1496&quot;\n      }\n    },\n    &quot;phone&quot;: &quot;1-770-736-8031 x56442&quot;,\n    &quot;website&quot;: &quot;hildegard.org&quot;,\n    &quot;company&quot;: {\n      &quot;name&quot;: &quot;Romaguera-Crona&quot;,\n      &quot;catchPhrase&quot;: &quot;Multi-layered client-server neural-net&quot;,\n      &quot;bs&quot;: &quot;harness real-time e-markets&quot;\n    }\n  },\n  ...\n  ...\n  ...\n  ...\n  ]\n<\/code><\/pre>\n<p>First, we create a digger, switch it to debug mode, and set Firefox as the name of the browser.<\/p>\n<pre class=\"language-yaml line-numbers\"><code class=\"language-yaml\">---\nconfig:\n    debug: 2\n    agent: Firefox<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blog.diggernaut.ru\/wp-content\/uploads\/2016\/09\/debug.jpg\" alt=\"ALT\"><\/p>\n<p>Let\u2019s add <strong>do<\/strong> block and use <strong>walk<\/strong> command with the address of the test RestAPI service inside. Also, I am going to show you a little trick that should be very useful in the future. The essence of the trick is to stop the execution of digger at some point you need. To do this, add the command <strong>stop<\/strong> to the <strong>do<\/strong>  block.<\/p>\n<pre class=\"language-yaml line-numbers\"><code class=\"language-yaml\">---\nconfig:\n    debug: 2\n    agent: Firefox\ndo:\n- walk:\n    to: http:\/\/jsonplaceholder.typicode.com\/users\n    do: \n    - stop<\/code><\/pre>\n<p>Run digger, and wait for the completion. As expected, the digger finished with an error.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.diggernaut.ru\/wp-content\/uploads\/2016\/09\/Failed.jpg\" alt=\"ALT\"><\/p>\n<p>Let us move to the digger\u2019s log and see why it happened.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.diggernaut.ru\/wp-content\/uploads\/2016\/09\/errorlog.jpg\" alt=\"ALT\"><\/p>\n<p><code>Unsupported operator used: stop<\/code><\/p>\n<p>The system does not support this command, it raised an exception, and stopped the execution of digger, as a digger is in debug mode, and the number of errors cannot be more than 1.<\/p>\n<p>Now let\u2019s see how server response was handled by the digger.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.diggernaut.ru\/wp-content\/uploads\/2016\/09\/jsonresp.jpg\" alt=\"ALT\"><\/p>\n<p>Its how it looks in text editor.<\/p>\n<pre><code class=\"language-html\"><html>\n    <head><\/head>\n    <body>\n        <body_safe>\n            <element>    \n                <address>      \n                    <city>Gwenborough<\/city>      \n                    <geo>        \n                        <lat>-37.3159<\/lat>        \n                        <lng>81.1496<\/lng>      \n                    <\/geo>      \n                    <street>Kulas Light<\/street>      \n                    <suite>Apt. 556<\/suite>     \n                    <zipcode>92998-3874<\/zipcode>    \n                <\/address>    \n                <company>     \n                    <bs>harness real-time e-markets<\/bs>      \n                    <catchphrase>Multi-layered client-server neural-net<\/catchphrase>     \n                    <name>Romaguera-Crona<\/name>    \n                    <\/company>    \n                <email>Sincere@april.biz<\/email>    \n                <id>1<\/id>    \n                <name>Leanne Graham<\/name>    \n                <phone>1-770-736-8031 x56442<\/phone>    \n                <username>Bret<\/username>    \n                <website>hildegard.org<\/website>  \n            <\/element>  \n            <element>    \n                <address>      \n                    <city>Wisokyburgh<\/city>      \n                    <geo>        \n                        <lat>-43.9509<\/lat>        \n                        <lng>-34.4618<\/lng>      \n                    <\/geo>      \n                    <street>Victor Plains<\/street>      \n                    <suite>Suite 879<\/suite>      \n                    <zipcode>90566-7771<\/zipcode>    \n                <\/address>    \n                <company>      \n                    <bs>synergize scalable supply-chains<\/bs>      \n                    <catchphrase>Proactive didactic contingency<\/catchphrase>      \n                    <name>Deckow-Crist<\/name>    \n                <\/company>    \n                <email>Shanna@melissa.tv<\/email>    \n                <id>2<\/id>    \n                <name>Ervin Howell<\/name>    \n                <phone>010-692-6593 x09125<\/phone>    \n                <username>Antonette<\/username>    \n                <website>anastasia.net<\/website>  \n            <\/element>  \n            ...\n            ...\n            ...\n            ...\n            ...\n            ...\n            ...\n            ...\n            ...\n        <\/body_safe>\n    <\/body>\n<\/html>\n<\/code><\/pre>\n<p>Let\u2019s delete the command <strong>stop<\/strong> from out config and go to each <strong>element<\/strong> tag and extract and collect data from it. It\u2019s simple. You can do it the same way as you do it with standard HTML, so I\u2019ll describe it very briefly using comments inside config. Comments are marked with a # character.<\/p>\n<pre class=\"language-yaml line-numbers\"><code class=\"language-yaml\">---\nconfig:\n    debug: 2\n    agent: Firefox\ndo:\n- walk:\n    to: http:\/\/jsonplaceholder.typicode.com\/users\n    do: \n    # Lets find all element tags and traverse through them\n    - find:\n        path: element\n        do:\n                Let&#039;s create an object to store data on each iteration for the element tag\n        - object_new: user\n        #Collecting data\n        #We will extract not all data, just few fields\n        - find:\n            path: id\n            do:\n            - parse\n            #Saving value of the register to the field &quot;id&quot; of the object &quot;user&quot; (futher we omit such comments as all operations are similar to this)\n            - object_field_set:\n                object: user\n                field: id\n        - find:\n            path: name\n            do:\n            - parse\n            - object_field_set:\n                object: user\n                field: name\n        - find:\n            path: username\n            do:\n            - parse\n            - object_field_set:\n                object: user\n                field: username\n        #Lets make full address merged to single field (we will use joinby option for it)\n        - find:\n            path: address\n            do:\n            - find:\n                path: street\n                do:\n                - parse\n                - object_field_set:\n                    object: user\n                    field: address\n            - find:\n                path: suite\n                do:\n                - parse\n                - object_field_set:\n                    object: user\n                    field: address\n                    joinby: &quot;, &quot;\n            - find:\n                path: city\n                do:\n                - parse\n                - object_field_set:\n                    object: user\n                    field: address\n                    joinby: &quot;, &quot;\n            - find:\n                path: zipcode\n                do:\n                - parse\n                - object_field_set:\n                    object: user\n                    field: address\n                    joinby: &quot;, &quot;\n        #Save object &quot;user&quot;\n        - object_save:\n            name: user<\/code><\/pre>\n<p>So we built our config and can now switch digger to active mode and run it.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.diggernaut.ru\/wp-content\/uploads\/2016\/09\/active.jpg\" alt=\"ALT\"><\/p>\n<p>When it\u2019s done, we can download data and check if all worked well. I downloaded data as JSON, and it looks this way in the text editor:<\/p>\n<pre><code class=\"language-js\">{\n    &quot;user&quot;: [{\n        &quot;username&quot;: &quot;Bret&quot;,\n        &quot;id&quot;: &quot;1&quot;,\n        &quot;name&quot;: &quot;Leanne Graham&quot;,\n        &quot;address&quot;: &quot;Kulas Light, Apt. 556, Gwenborough, 92998-3874&quot;\n    },\n    ...\n    ...\n    ...\n     {\n        &quot;username&quot;: &quot;Moriah.Stanton&quot;,\n        &quot;address&quot;: &quot;Kattie Turnpike, Suite 198, Lebsackbury, 31428-2261&quot;,\n        &quot;id&quot;: &quot;10&quot;,\n        &quot;name&quot;: &quot;Clementina DuBuque&quot;\n    }],\n    &quot;session&quot;: &quot;session_595&quot;,\n    &quot;digger&quot;: &quot;digger_120&quot;\n}\n<\/code><\/pre>\n<p>That\u2019s all, if you have any questions, feel free to ask in comments, I will answer.<\/p>","protected":false},"excerpt":{"rendered":"<p>Diggernaut makes it easy to work with JSON format by converting it to XML. So now I\u2019ll show you how you can work with it. I found one of the public JSON Rest API we use as a sample. Link. It\u2019s a sample of their server response: [ { &#8220;id&#8221;: 1, &#8220;name&#8221;: &#8220;Leanne Graham&#8221;, &#8220;username&#8221;: [&hellip;]<\/p>","protected":false},"author":5,"featured_media":58,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learning-meta-language","category-web-scraping"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":11,"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":686,"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions\/686"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/media\/58"}],"wp:attachment":[{"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.diggernaut.com\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}