{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "chem-bla-ics",
  "description": "Chemblaics (pronounced chem-bla-ics) is the science that uses open science and computers to solve problems in chemistry, biochemistry and related fields.",
  "home_page_url": "https://chem-bla-ics.linkedchemistry.info/",
  "feed_url": "https://chem-bla-ics.linkedchemistry.info/2014/11/16/programming-in-life-sciences-20.json",
  "icon": "https://chem-bla-ics.linkedchemistry.info/assets/images/chem-bla-ics_logo.png",
  "language": "en",
  "authors": [
    {
      "name": "Egon Willighagen",
      "url": "https://orcid.org/0000-0001-7542-0286",
      "_orcid": "0000-0001-7542-0286"
    }
  ],
  "items": [

    {
      "id": "https://doi.org/10.59350/szpy6-v8c44",
      "url": "https://chem-bla-ics.linkedchemistry.info/2014/11/16/programming-in-life-sciences-20.html",
      "title": "Programming in the Life Sciences #20: extracting data from JSON",
      "content_html": "<p>I previously wrote about the <a href=\"http://chem-bla-ics.blogspot.nl/2013/10/programming-in-life-sciences-10.html\">JavaScript Object Notation</a>\n(JSON) which has become a de facto standard for sharing data by web services. I personally\nstill prefer something using the <a href=\"https://en.wikipedia.org/wiki/Resource_Description_Framework\">Resource Description Framework</a>\n(RDF) because of its clear link to ontologies, but perhaps\n<a href=\"https://en.wikipedia.org/wiki/JSON-LD\">JSON-LD</a> combines the best of both worlds.</p>\n\n<p>The <a href=\"https://dev.openphacts.org/\">Open PHACTS API</a> support various formats and this\nJSON is the default format used by the <a href=\"https://github.com/openphacts/ops.js\">ops.js</a>\nlibrary. However, the amount of information returned by the Open PHACTS cache is complex,\nand generally includes more than you want to use in the next step. Therefore, it is\nneeded to extract data from the JSON document, which was not covered in the\n<a href=\"http://chem-bla-ics.blogspot.nl/2013/10/programming-in-life-sciences-10.html\">post #10</a>\n<a href=\"http://chem-bla-ics.blogspot.nl/2013/10/programming-in-life-sciences-11-html.html\">or #11</a>.</p>\n\n<p>Let’s start with the example JSON given in that post, and let’s consider this is the\nvalue of a variable with the name jsonData:</p>\n\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{</span><span class=\"w\">\n    </span><span class=\"nl\">\"id\"</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"w\">\n    </span><span class=\"nl\">\"name\"</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"s2\">\"Foo\"</span><span class=\"p\">,</span><span class=\"w\">\n    </span><span class=\"nl\">\"price\"</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">123</span><span class=\"p\">,</span><span class=\"w\">\n    </span><span class=\"nl\">\"tags\"</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"p\">[</span><span class=\"w\"> </span><span class=\"s2\">\"Bar\"</span><span class=\"p\">,</span><span class=\"w\"> </span><span class=\"s2\">\"Eek\"</span><span class=\"w\"> </span><span class=\"p\">],</span><span class=\"w\">\n    </span><span class=\"nl\">\"stock\"</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"p\">{</span><span class=\"w\">\n        </span><span class=\"nl\">\"warehouse\"</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">300</span><span class=\"p\">,</span><span class=\"w\">\n        </span><span class=\"nl\">\"retail\"</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">20</span><span class=\"w\">\n    </span><span class=\"p\">}</span><span class=\"w\">\n</span><span class=\"p\">}</span><span class=\"w\">\n</span></code></pre></div></div>\n\n<p>We can see that this JSON value starts with a map-like structure. We can also see that\nthere is a list embedded, and another map. I guess that one of the reasons why JSON\nhas taken such a flight is how well it integrates with the JavaScript language: selecting\ncontent can be done in terms of core language features, different from, for example,\n<a href=\"https://en.wikipedia.org/wiki/XPath\">XPath</a> statements needed for\n<a href=\"https://en.wikipedia.org/wiki/XML\">XML</a> or <a href=\"https://en.wikipedia.org/wiki/SPARQL\">SPARQL</a>\nfor RDF content. This is because the notation just follows core data types of JavaScript\nand data is stored as native data types and objects.</p>\n\n<p>For example, to get the price value from the above JSON code, we use:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">price</span> <span class=\"o\">=</span> <span class=\"nx\">jsonData</span><span class=\"p\">.</span><span class=\"nx\">price</span><span class=\"p\">;</span>\n</code></pre></div></div>\n\n<p>Or, if we want to get the first value in the Bar-Eek list, we use:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">tag</span> <span class=\"o\">=</span> <span class=\"nx\">jsonData</span><span class=\"p\">.</span><span class=\"nx\">tags</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">];</span>\n</code></pre></div></div>\n\n<p>Or, if we want to inspect the warehouse stock:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">inStock</span> <span class=\"o\">=</span> <span class=\"nx\">jsonData</span><span class=\"p\">.</span><span class=\"nx\">stock</span><span class=\"p\">.</span><span class=\"nx\">warehouse</span><span class=\"p\">;</span>\n</code></pre></div></div>\n\n<p>Now, the JSON returned by the Open PHACTS API has a lot more information. This is why the\nonline, interactive documentation is so helpful: it shows the JSON. In fact, given that\nJSON is so much used, there are many tools online that help you, such as\n<a href=\"http://jsoneditoronline.org/\">jsoneditoronline.org</a> (yes, it will show error messages\nif the syntax is wrong):</p>\n\n<p><img src=\"/assets/images/debug3.png\" alt=\"\" /></p>\n\n<p>BTW, I also recommend installing a JSON viewer extension for\n<a href=\"https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc?hl=en#sthash.vsIhyalK.dpuf\">Chrome</a>\nor for <a href=\"https://addons.mozilla.org/en-US/firefox/addon/jsonview/\">Firefox</a>. Once you\nhave installed this plugin, you can not just read the JSON on Open PHACTS’\ninteractive documentation page, but also open the Request URL into a separate browser\nwindow. Just copy/paste the URL from this output:</p>\n\n<p><img src=\"/assets/images/json.png\" alt=\"\" /></p>\n\n<p>And with a JSON viewing extension, opening this <em>https://beta.openphacts.org/1.3/pathways/…</em>\nURL in your browser window will look something like:</p>\n\n<p><img src=\"/assets/images/json1.png\" alt=\"\" /></p>\n\n<p>And because these extensions typically use syntax highlighting, it is easier to understand\nhow to access information from within your JavaScript code. For example, if we want the\nnumber of pathways in which the compound <a href=\"http://www.conceptwiki.org/concept/index/7e0a4dd4-8160-4906-9db1-fdb300e888ea\">testosterone</a>\n(the link is the <a href=\"http://scholar.google.com/scholar?hl=nl&amp;q=ConceptWiki\">ConceptWiki</a>\nURL in the above example) is found, we can use this code:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">pathwayCount</span> <span class=\"o\">=</span> <span class=\"nx\">jsonData</span><span class=\"p\">.</span><span class=\"nx\">result</span><span class=\"p\">.</span><span class=\"nx\">primaryTopic</span><span class=\"p\">.</span><span class=\"nx\">pathway_count</span><span class=\"p\">;</span>\n</code></pre></div></div>",
      "summary": "I previously wrote about the JavaScript Object Notation (JSON) which has become a de facto standard for sharing data by web services. I personally still prefer something using the Resource Description Framework (RDF) because of its clear link to ontologies, but perhaps JSON-LD combines the best of both worlds.",
      "image": "https://chem-bla-ics.linkedchemistry.info/assets/images/debug3.png",
      "date_published": "2014-11-16T00:00:00+00:00",
      "date_modified": "2025-02-15T00:00:00+00:00",
      "tags": ["pra3006"],
      
      
      
      
      
      
        "authors": [ { "name": "Egon Willighagen", "url": "https://orcid.org/0000-0001-7542-0286" } ]
      
    }

  ]
}
