<p>Here a <code>jq</code> solution if you can use it:</p>
<pre class="lang-bash prettyprint-override"><code>jq -r --arg f &quot;eQTLs&quot; '$f + &quot;\t&quot; + ([.&quot;@graph&quot;[] | select(.method == $f) | .value] | join(&quot;, &quot;))' file.json
</code></pre>
<p>Explanations:</p>
<ul>
<li><code>-r</code>: Raw string result (so result string will be without between <code>&quot;</code>)</li>
<li><code>--arg name value</code>: Your filter on <code>eQTLs</code> method value</li>
<li>Search in <code>@graph</code> array elements with <code>[]</code></li>
<li>Select only array element with <code>method</code> attribute equals to <code>eQTLs</code> (in <code>$f</code> argument)</li>
<li>The result, 2 in this case, are put in a array <code>[...]</code> and join with string <code>, </code></li>
<li><code>$f</code> string, a tabulation <code>&quot;\t&quot;</code> and the join result are concatenated to one string with <code>+</code> operators</li>
</ul>
<p>Result:</p>
<pre><code>eQTLs   EIF3S8, EIF3CL
</code></pre>

CC BY-SA 4.0
2022-11-09T11:41:41.097Z
2022-11-09T20:37:40.473Z
2022-11-09T20:37:40.473Z
Arnaud Valmary
<p>A (very) simple Python solution:</p>
<p>The script file <code>readfile.py</code>:</p>
<pre class="lang-py prettyprint-override"><code>#! /usr/bin/python3

import sys
import json

filename=sys.argv[1]
filter=sys.argv[2]

results=[]
with open(filename) as json_file:
    json_content = json.load(json_file)
    for json_graph_element in json_content[&quot;@graph&quot;]:
        if json_graph_element[&quot;method&quot;] == filter:
            results.append(json_graph_element[&quot;value&quot;])
print(&quot;%s\t%s&quot; % (filter, &quot;, &quot;.join(results)))
</code></pre>
<p>Make the script executable:</p>
<pre class="lang-bash prettyprint-override"><code>chmod +x readfile.py
</code></pre>
<p>Use the script:</p>
<pre class="lang-bash prettyprint-override"><code>./readfile.py /path/to/the/file.json  eQTLs
</code></pre>
<p>Result:</p>
<pre><code>eQTLs   EIF3S8, EIF3CL
</code></pre>

CC BY-SA 4.0
2022-11-10T10:10:23.450Z
2022-11-10T10:10:23.450Z
Arnaud Valmary
<p>It's a little hard to know what your goal is here based on the sample code, but I do find that your JSON snippet does not match any spec. I was able to edit it to add brackets to make the entries elements of a list:</p>
<pre><code>[
  {
  &quot;biosample_ontology&quot;: {
    &quot;@id&quot;: &quot;https://www.encodeproject.org/biosample-types/primary_cell_CL_0000576/&quot;,
    &quot;@type&quot;: [
      &quot;BiosampleType&quot;,
      &quot;Item&quot;
    ],
    &quot;aliases&quot;: [],
    &quot;cell_slims&quot;: [
      &quot;hematopoietic cell&quot;,
      &quot;monocyte&quot;,
      &quot;myeloid cell&quot;,
      &quot;leukocyte&quot;
    ],
    &quot;classification&quot;: &quot;primary cell&quot;,
    &quot;dbxrefs&quot;: [],
    &quot;developmental_slims&quot;: [
      &quot;mesoderm&quot;
    ],
    &quot;name&quot;: &quot;primary_cell_CL_0000576&quot;,
    &quot;organ_slims&quot;: [
      &quot;blood&quot;,
      &quot;bodily fluid&quot;
    ],
    &quot;references&quot;: [],
    &quot;schema_version&quot;: &quot;1&quot;,
    &quot;status&quot;: &quot;released&quot;,
    &quot;synonyms&quot;: [],
    &quot;system_slims&quot;: [
      &quot;immune system&quot;
    ],
    &quot;term_id&quot;: &quot;CL:0000576&quot;,
    &quot;term_name&quot;: &quot;monocyte&quot;,
    &quot;uuid&quot;: &quot;20685264-1187-4c5c-b4ce-dc85ea53fc11&quot;
  },
  &quot;chrom&quot;: &quot;chr11&quot;,
  &quot;dataset&quot;: &quot;https://www.encodeproject.org/annotations/ENCSR464XOM/&quot;,
  &quot;dataset_rel&quot;: &quot;/annotations/ENCSR464XOM/&quot;,
  &quot;documents&quot;: [],
  &quot;end&quot;: 59936979,
  &quot;file&quot;: &quot;ENCFF814NRI&quot;,
  &quot;method&quot;: &quot;eQTLs&quot;,
  &quot;start&quot;: 59936978,
  &quot;strand&quot;: null,
  &quot;targets&quot;: [],
  &quot;value&quot;: &quot;MS4A4A&quot;
},

    {
  &quot;biosample_ontology&quot;: {
    &quot;@id&quot;: &quot;https://www.encodeproject.org/biosample-types/primary_cell_CL_0000576/&quot;,
    &quot;@type&quot;: [
      &quot;BiosampleType&quot;,
      &quot;Item&quot;
    ],
    &quot;aliases&quot;: [],
    &quot;cell_slims&quot;: [
      &quot;hematopoietic cell&quot;,
      &quot;monocyte&quot;,
      &quot;myeloid cell&quot;,
      &quot;leukocyte&quot;
    ],
    &quot;classification&quot;: &quot;primary cell&quot;,
    &quot;dbxrefs&quot;: [],
    &quot;developmental_slims&quot;: [
      &quot;mesoderm&quot;
    ],
    &quot;name&quot;: &quot;primary_cell_CL_0000576&quot;,
    &quot;organ_slims&quot;: [
      &quot;blood&quot;,
      &quot;bodily fluid&quot;
    ],
    &quot;references&quot;: [],
    &quot;schema_version&quot;: &quot;1&quot;,
    &quot;status&quot;: &quot;released&quot;,
    &quot;synonyms&quot;: [],
    &quot;system_slims&quot;: [
      &quot;immune system&quot;
    ],
    &quot;term_id&quot;: &quot;CL:0000576&quot;,
    &quot;term_name&quot;: &quot;monocyte&quot;,
    &quot;uuid&quot;: &quot;20685264-1187-4c5c-b4ce-dc85ea53fc11&quot;
  },
  &quot;chrom&quot;: &quot;chr11&quot;,
  &quot;dataset&quot;: &quot;https://www.encodeproject.org/annotations/ENCSR464XOM/&quot;,
  &quot;dataset_rel&quot;: &quot;/annotations/ENCSR464XOM/&quot;,
  &quot;documents&quot;: [],
  &quot;end&quot;: 59936979,
  &quot;file&quot;: &quot;ENCFF814NRI&quot;,
  &quot;method&quot;: &quot;eQTLs&quot;,
  &quot;start&quot;: 59936979,
  &quot;strand&quot;: null,
  &quot;targets&quot;: [],
  &quot;value&quot;: &quot;MS4A4B&quot;
}
]
</code></pre>
<p>When I did this, I was able to slightly modify your code to print out each element's <code>method</code> key, value pair:</p>
<pre><code>import json
with open('/Users/max.press/Downloads/file.json') as f:
    f_json = json.load(f)
    for i in range(len(f_json)):
        print(f_json[i][&quot;method&quot;] + &quot;\t&quot; + f_json[i][&quot;value&quot;])
</code></pre>
<pre><code>% python ~/Downloads/readfile.py
# prints this:
eQTLs   MS4A4A
eQTLs   MS4A4B
</code></pre>

CC BY-SA 4.0
2022-11-09T09:08:22.133Z
Hi Maximilian Press, I am sorry the code doesn't work, with error code as followings.

print(f_json[i]["method"] + "\t" + f_json[i]["value"])
KeyError: 0
austin7923
CC BY-SA 4.0
2022-11-09T09:09:22.473Z
I am sorry about the JSON snippet format. Let me reupload a complete one to the original question. Could you please try again? Thank you.
austin7923
CC BY-SA 4.0
2022-11-09T17:54:13.130Z
@austin7923 it looks like there was additional formatting in your json outside of the snippet that you first posted, which makes the outer layer into a dict within which the list is nested. Thus the keyerror. regardless it looks like the jq solution works for you.
Maximilian Press
CC BY-SA 4.0
2022-11-09T07:57:19.360Z
2022-11-09T07:57:19.360Z
Maximilian Press
<p>I have a long list of JSON data, with repeats of contents similar to followings.</p>
<p>Due to the original JSON file is too long, I will just shared the hyperlinks here. This is a result generated from a database called RegulomeDB.</p>
<p><a href="https://regulomedb.org/regulome-search/?regions=chr16:28539847-28539848&amp;genome=GRCh37&amp;format=json" rel="nofollow noreferrer">Direct link to the JSON file</a></p>
<p>I would like to extract specific data (eQTLs) from <code>&quot;method&quot;: &quot;eQTLs&quot;</code> and <code>&quot;value&quot;: &quot;xxxx&quot;</code>, and put them into 2 columns (tab delimited) exactly like below.
Note: <code>&quot;value&quot;:&quot;xxxx&quot;</code> is extracted right after <code>&quot;method&quot;: &quot;eQTLs&quot;</code>is detected.</p>
<pre><code>eQTLs   firstResult, secondResult, thirdResult, ...
</code></pre>
<p>In this example, the desired output is:</p>
<pre><code>eQTLs   EIF3S8, EIF3CL
</code></pre>
<p>I've tried using a python script but was unsuccessful.</p>
<pre><code>import json
with open('file.json') as f:
    f_json = json.load(f)
    print 'f_json[0]['&quot;method&quot;: &quot;eQTLs&quot;'] + &quot;\t&quot; + f_json[0][&quot;value&quot;]
</code></pre>
<p>Thank you for your kind help.</p>

CC BY-SA 4.0
2022-11-09T09:27:42.470Z
Could you clarify what output format you expect please? You say "tab delimited" but show comma delimited data.
terdon
CC BY-SA 4.0
2022-11-09T09:50:11.030Z
HI @terdon, edited my post. Thank you.
austin7923
<python><bash><text-processing><data-retrieval>
eQTL data extraction from database JSON file
# eQTL data extraction from database JSON file

(Asked by: username_0 on 2022-11-09)

I have a long list of JSON data, with repeats of contents similar to followings.
Due to the original JSON file is too long, I will just shared the hyperlinks here. This is a result generated from a database called RegulomeDB.
Direct link to the JSON file
I would like to extract specific data (eQTLs) from "method": "eQTLs" and "value": "xxxx", and put them into 2 columns (tab delimited) exactly like below.
Note: "value":"xxxx" is extracted right after "method": "eQTLs"is detected.
eQTLs   firstResult, secondResult, thirdResult, ...

In this example, the desired output is:
eQTLs   EIF3S8, EIF3CL

I've tried using a python script but was unsuccessful.
import json
with open('file.json') as f:
    f_json = json.load(f)
    print 'f_json[0]['"method": "eQTLs"'] + "\t" + f_json[0]["value"]

Thank you for your kind help.

**Tags:** python, bash, text-processing, data-retrieval
> **Comments:**
> **username_1**: Could you clarify what output format you expect please? You say "tab delimited" but show comma delimited data.  
> **username_0**: HI @terdon, edited my post. Thank you.  

---

## 3 answers

### Answer by username_2 on 2022-11-09. Score: 3 | Accepted answer

Here a jq solution if you can use it:
jq -r --arg f "eQTLs" '$f + "\t" + ([."@graph"[] | select(.method == $f) | .value] | join(", "))' file.json

Explanations:

-r: Raw string result (so result string will be without between ")
--arg name value: Your filter on eQTLs method value
Search in @graph array elements with []
Select only array element with method attribute equals to eQTLs (in $f argument)
The result, 2 in this case, are put in a array [...] and join with string , 
$f string, a tabulation "\t" and the join result are concatenated to one string with + operators

Result:
eQTLs   EIF3S8, EIF3CL


---

### Answer by username_2 on 2022-11-10. Score: 0

A (very) simple Python solution:
The script file readfile.py:
#! /usr/bin/python3

import sys
import json

filename=sys.argv[1]
filter=sys.argv[2]

results=[]
with open(filename) as json_file:
    json_content = json.load(json_file)
    for json_graph_element in json_content["@graph"]:
        if json_graph_element["method"] == filter:
            results.append(json_graph_element["value"])
print("%s\t%s" % (filter, ", ".join(results)))

Make the script executable:
chmod +x readfile.py

Use the script:
./readfile.py /path/to/the/file.json  eQTLs

Result:
eQTLs   EIF3S8, EIF3CL


---

### Answer by username_3 on 2022-11-09. Score: 0

It's a little hard to know what your goal is here based on the sample code, but I do find that your JSON snippet does not match any spec. I was able to edit it to add brackets to make the entries elements of a list:
[
  {
  "biosample_ontology": {
    "@id": "https://www.encodeproject.org/biosample-types/primary_cell_CL_0000576/",
    "@type": [
      "BiosampleType",
      "Item"
    ],
    "aliases": [],
    "cell_slims": [
      "hematopoietic cell",
      "monocyte",
      "myeloid cell",
      "leukocyte"
    ],
    "classification": "primary cell",
    "dbxrefs": [],
    "developmental_slims": [
      "mesoderm"
    ],
    "name": "primary_cell_CL_0000576",
    "organ_slims": [
      "blood",
      "bodily fluid"
    ],
    "references": [],
    "schema_version": "1",
    "status": "released",
    "synonyms": [],
    "system_slims": [
      "immune system"
    ],
    "term_id": "CL:0000576",
    "term_name": "monocyte",
    "uuid": "20685264-1187-4c5c-b4ce-dc85ea53fc11"
  },
  "chrom": "chr11",
  "dataset": "https://www.encodeproject.org/annotations/ENCSR464XOM/",
  "dataset_rel": "/annotations/ENCSR464XOM/",
  "documents": [],
  "end": 59936979,
  "file": "ENCFF814NRI",
  "method": "eQTLs",
  "start": 59936978,
  "strand": null,
  "targets": [],
  "value": "MS4A4A"
},

    {
  "biosample_ontology": {
    "@id": "https://www.encodeproject.org/biosample-types/primary_cell_CL_0000576/",
    "@type": [
      "BiosampleType",
      "Item"
    ],
    "aliases": [],
    "cell_slims": [
      "hematopoietic cell",
      "monocyte",
      "myeloid cell",
      "leukocyte"
    ],
    "classification": "primary cell",
    "dbxrefs": [],
    "developmental_slims": [
      "mesoderm"
    ],
    "name": "primary_cell_CL_0000576",
    "organ_slims": [
      "blood",
      "bodily fluid"
    ],
    "references": [],
    "schema_version": "1",
    "status": "released",
    "synonyms": [],
    "system_slims": [
      "immune system"
    ],
    "term_id": "CL:0000576",
    "term_name": "monocyte",
    "uuid": "20685264-1187-4c5c-b4ce-dc85ea53fc11"
  },
  "chrom": "chr11",
  "dataset": "https://www.encodeproject.org/annotations/ENCSR464XOM/",
  "dataset_rel": "/annotations/ENCSR464XOM/",
  "documents": [],
  "end": 59936979,
  "file": "ENCFF814NRI",
  "method": "eQTLs",
  "start": 59936979,
  "strand": null,
  "targets": [],
  "value": "MS4A4B"
}
]

When I did this, I was able to slightly modify your code to print out each element's method key, value pair:
import json
with open('/Users/max.press/Downloads/file.json') as f:
    f_json = json.load(f)
    for i in range(len(f_json)):
        print(f_json[i]["method"] + "\t" + f_json[i]["value"])

% python ~/Downloads/readfile.py
# prints this:
eQTLs   MS4A4A
eQTLs   MS4A4B

> **Comments:**
> **username_0**: Hi Maximilian Press, I am sorry the code doesn't work, with error code as followings.  print(f_json[i]["method"] + "\t" + f_json[i]["value"]) KeyError: 0  
> **username_0**: I am sorry about the JSON snippet format. Let me reupload a complete one to the original question. Could you please try again? Thank you.  
> **username_3**: @austin7923 it looks like there was additional formatting in your json outside of the snippet that you first posted, which makes the outer layer into a dict within which the list is nested. Thus the keyerror. regardless it looks like the jq solution works for you.  

---

