<p>I think you are using an outdated version of the API, you need to change the <code>&quot;field&quot;</code> with <code>cases.project.project_id</code></p>
<pre><code>import requests
import json

cases_endpt = &quot;https://api.gdc.cancer.gov/cases&quot;

fields = [
    &quot;sample_id&quot;,
    &quot;sample_uuid&quot;,
    &quot;sample_type&quot;,
    &quot;sample_type_id&quot;,
    &quot;tissue_type&quot;,
    &quot;tumor_code&quot;,
    &quot;tumor_code_id&quot;,
    &quot;oct_embedded&quot;,
    &quot;shortest_dimension&quot;,
    &quot;intermediate dimension&quot;,
    &quot;longest dimension&quot;,
    &quot;is_ffpe&quot;,
    &quot;pathology_report_uuid&quot;,
    &quot;tumor_descriptor&quot;,
    &quot;current weight&quot;,
    &quot;initial weight&quot;,
    &quot;composition&quot;,
    &quot;time_between_clamping_and_freezing&quot;,
    &quot;time_between_excision_and_freezing&quot;,
    &quot;days_to_sample_procurement&quot;,
    &quot;freezing_method&quot;,
    &quot;preservation_method&quot;,
    &quot;days_to_collection&quot;,
    &quot;portions&quot;,
]

fields = &quot;,&quot;.join(fields)

filters = {
    &quot;op&quot;: &quot;in&quot;,
    &quot;content&quot;: {&quot;field&quot;: &quot;cases.project.project_id&quot;,
                &quot;value&quot;: [&quot;TCGA-BRCA&quot;]},
}

params = {&quot;filters&quot;: json.dumps(filters), 
          &quot;format&quot;: &quot;TSV&quot;, 
          &quot;size&quot;: &quot;10000&quot;}

response = requests.get(cases_endpt, params=params)
content = str(response.content, &quot;utf-8&quot;)

with open(&quot;query10.tsv&quot;, &quot;w&quot;) as file:
    file.write(content)
</code></pre>
<p>And if you use <em>with open</em>, you don't need to use <em>close</em>.</p>

CC BY-SA 4.0
2021-06-14T20:33:51.117Z
2021-06-14T20:33:51.117Z
zorbax
<p>I have been trying to access the GDC metadata with python through the filter below. However, this code I have been using always yields a blank tsv file. Any help on fixing the code is appreciated!</p>
<pre><code>import requests
import json

cases_endpt = 'https://api.gdc.cancer.gov/cases'

# The 'fields' parameter is passed as a comma-separated string of single names.
fields = [
    &quot;sample_id&quot;,
    &quot;sample_uuid&quot;,
    &quot;sample_type&quot;,
    &quot;sample_type_id&quot;,
    &quot;tissue_type&quot;,
    &quot;tumor_code&quot;,
    &quot;tumor_code_id&quot;,
    &quot;oct_embedded&quot;,
    &quot;shortest_dimension&quot;,
    &quot;intermediate dimension&quot;,
    &quot;longest dimension&quot;,
    &quot;is_ffpe&quot;,
    &quot;pathology_report_uuid&quot;,
    &quot;tumor_descriptor&quot;,
    &quot;current weight&quot;,
    &quot;initial weight&quot;,
    &quot;composition&quot;,
    &quot;time_between_clamping_and_freezing&quot;,
    &quot;time_between_excision_and_freezing&quot;,
    &quot;days_to_sample_procurement&quot;,
    &quot;freezing_method&quot;,
    &quot;preservation_method&quot;,
    &quot;days_to_collection&quot;,
    &quot;portions&quot;
    ]

    fields = ','.join(fields)

filters = {
    &quot;op&quot;: &quot;in&quot;,
    &quot;content&quot;: {
        &quot;field&quot;: &quot;project_id&quot;,
        &quot;value&quot;: [&quot;TCGA-BRCA&quot;]
    }
 }

params = {
    &quot;filters&quot;: json.dumps(filters),
    &quot;fields&quot;: fields,
    &quot;format&quot;: &quot;TSV&quot;,
    &quot;size&quot;: &quot;10000&quot;
    }

response = requests.get(cases_endpt, params=params)

file = open(&quot;query10.tsv&quot;, &quot;w&quot;)
file.write(response.text)
file.close()
</code></pre>

<python>
Querying metadata (GDC) using a filter
# Querying metadata (GDC) using a filter

(Asked by: username_0 on 2021-06-14)

I have been trying to access the GDC metadata with python through the filter below. However, this code I have been using always yields a blank tsv file. Any help on fixing the code is appreciated!
import requests
import json

cases_endpt = 'https://api.gdc.cancer.gov/cases'

# The 'fields' parameter is passed as a comma-separated string of single names.
fields = [
    "sample_id",
    "sample_uuid",
    "sample_type",
    "sample_type_id",
    "tissue_type",
    "tumor_code",
    "tumor_code_id",
    "oct_embedded",
    "shortest_dimension",
    "intermediate dimension",
    "longest dimension",
    "is_ffpe",
    "pathology_report_uuid",
    "tumor_descriptor",
    "current weight",
    "initial weight",
    "composition",
    "time_between_clamping_and_freezing",
    "time_between_excision_and_freezing",
    "days_to_sample_procurement",
    "freezing_method",
    "preservation_method",
    "days_to_collection",
    "portions"
    ]

    fields = ','.join(fields)

filters = {
    "op": "in",
    "content": {
        "field": "project_id",
        "value": ["TCGA-BRCA"]
    }
 }

params = {
    "filters": json.dumps(filters),
    "fields": fields,
    "format": "TSV",
    "size": "10000"
    }

response = requests.get(cases_endpt, params=params)

file = open("query10.tsv", "w")
file.write(response.text)
file.close()

**Tags:** python

---

## 1 answers

### Answer by username_1 on 2021-06-14. Score: 1 | Accepted answer

I think you are using an outdated version of the API, you need to change the "field" with cases.project.project_id
import requests
import json

cases_endpt = "https://api.gdc.cancer.gov/cases"

fields = [
    "sample_id",
    "sample_uuid",
    "sample_type",
    "sample_type_id",
    "tissue_type",
    "tumor_code",
    "tumor_code_id",
    "oct_embedded",
    "shortest_dimension",
    "intermediate dimension",
    "longest dimension",
    "is_ffpe",
    "pathology_report_uuid",
    "tumor_descriptor",
    "current weight",
    "initial weight",
    "composition",
    "time_between_clamping_and_freezing",
    "time_between_excision_and_freezing",
    "days_to_sample_procurement",
    "freezing_method",
    "preservation_method",
    "days_to_collection",
    "portions",
]

fields = ",".join(fields)

filters = {
    "op": "in",
    "content": {"field": "cases.project.project_id",
                "value": ["TCGA-BRCA"]},
}

params = {"filters": json.dumps(filters), 
          "format": "TSV", 
          "size": "10000"}

response = requests.get(cases_endpt, params=params)
content = str(response.content, "utf-8")

with open("query10.tsv", "w") as file:
    file.write(content)

And if you use with open, you don't need to use close.


---

