<p>There is a major issue about the <code>json</code> format required, however just converting to <code>json</code> is easy because <code>iterrows</code> has to be <code>pandas</code> and <code>pandas</code> has its own output <code>json</code> format ... For example,</p>
<pre><code>dataframe.to_json('benchling.json'', orient='records', lines=True)
</code></pre>
<p>If that fails then,</p>
<pre><code>jsonOut = dataframe.to_json(orient='records')[1:-1].replace('},{', '} {')
with open('benchling.json', 'w') as fout:
    fout.write(jsonOut)
</code></pre>
<p>However, I don't think it would work and at a minimum I think the headers of the <code>csv</code> will need to change.</p>
<hr />
<p>The error</p>
<blockquote>
<p>&quot;nucleotide_length&quot;: &quot;Value must be an integer&quot;</p>
</blockquote>
<p>Looking at your code ....</p>
<pre><code>&quot;nucleotide_length&quot;: {&quot;value&quot;: j['nucl_length']}
</code></pre>
<p>Sure this is right?</p>
<p>Why not try ...</p>
<pre><code>&quot;nucleotide_length&quot;: int(j['nucl_length'])
</code></pre>
<p>Thats what the error seems to be saying.</p>

CC BY-SA 4.0
2022-12-13T01:41:18.973Z
2022-12-13T01:41:18.973Z
M__
<p>I am writing a benchling uploader script in python to upload data from a csv file in a JSON formatted schema to the benchling API platform. My function to create the JSON schema in the script looks like the following:</p>
<pre><code>def create_antismash_schema(dataframe):
    &quot;&quot;&quot;Create a json object for antismash output&quot;&quot;&quot;
    return {
        &quot;assayResults&quot;: [
            {
                &quot;fields&quot;: {
                    &quot;entity&quot;: {&quot;value&quot;: j['SBI ID']},
                    &quot;sequence_id&quot;: {&quot;value&quot;: j['sequence_id']},
                    &quot;category&quot;: {&quot;value&quot;: j['Category']},
                    &quot;product&quot;: {&quot;value&quot;: j['Product']},
                    &quot;start&quot;: {&quot;value&quot;: j['start']},
                    &quot;end&quot;: {&quot;value&quot;: j['end']},
                    &quot;nucleotide_length&quot;: {&quot;value&quot;: j['nucl_length']}
                },
                &quot;schemaId&quot;: &quot;assaysch_Z6B7TlWG&quot;,
                &quot;projectId&quot;: 'src_tIUAcxDH'
            } for i, j in dataframe.iterrows()
        ]
    }    

</code></pre>
<p>Now the data I have in benchling for the fields matches the keys in the schema and the values match the headers in the csv file I'm pulling the data from.</p>
<p>benchling results schema looks like:</p>
<p><a href="https://i.sstatic.net/lLCb1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lLCb1.png" alt="enter image description here" /></a></p>
<p>csv file:</p>
<p><a href="https://i.sstatic.net/RMEX6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/RMEX6.png" alt="enter image description here" /></a></p>
<p>The error I am getting when I try to upload is the following:</p>
<pre><code>    
Uploading Antismash data to Benchling...
  0%|                                                                                                                                                                               | 0/159 [00:00&lt;?, ?it/s]{
    &quot;assayResults&quot;: null,
    &quot;errors&quot;: [
        {
            &quot;fields&quot;: {
                &quot;nucleotide_length&quot;: &quot;Value must be an integer&quot;
            },
            &quot;global&quot;: []
        },
        {
            &quot;fields&quot;: {
                &quot;nucleotide_length&quot;: &quot;Value must be an integer&quot;
            },
            &quot;global&quot;: []
        },
        {
            &quot;fields&quot;: {
                &quot;nucleotide_length&quot;: &quot;Value must be an integer&quot;
            },
            &quot;global&quot;: []
        }
    ]
}
  0%|                                                                                                                                                                               | 0/159 [00:00&lt;?, ?it/s]
Failed to upload summary.
HTTP status code: 400


</code></pre>
<p>The field is set to be an integer in benchling and all the values I'm pulling from in the csv file for the nucl_length column are integer values so not sure why this error is beig thrown. Any guidance here appreciated, thanks.</p>

<python><dataframe><pandas><json>
HTTP status code: 400 "Value must be an integer" error uploading JSON schema results to benchling
# HTTP status code: 400 "Value must be an integer" error uploading JSON schema results to benchling

(Asked by: username_0 on 2022-12-12)

I am writing a benchling uploader script in python to upload data from a csv file in a JSON formatted schema to the benchling API platform. My function to create the JSON schema in the script looks like the following:
def create_antismash_schema(dataframe):
    """Create a json object for antismash output"""
    return {
        "assayResults": [
            {
                "fields": {
                    "entity": {"value": j['SBI ID']},
                    "sequence_id": {"value": j['sequence_id']},
                    "category": {"value": j['Category']},
                    "product": {"value": j['Product']},
                    "start": {"value": j['start']},
                    "end": {"value": j['end']},
                    "nucleotide_length": {"value": j['nucl_length']}
                },
                "schemaId": "assaysch_Z6B7TlWG",
                "projectId": 'src_tIUAcxDH'
            } for i, j in dataframe.iterrows()
        ]
    }    


Now the data I have in benchling for the fields matches the keys in the schema and the values match the headers in the csv file I'm pulling the data from.
benchling results schema looks like:

csv file:

The error I am getting when I try to upload is the following:
    
Uploading Antismash data to Benchling...
  0%|                                                                                                                                                                               | 0/159 [00:00
The field is set to be an integer in benchling and all the values I'm pulling from in the csv file for the nucl_length column are integer values so not sure why this error is beig thrown. Any guidance here appreciated, thanks.

**Tags:** python, dataframe, pandas, json

---

## 1 answers

### Answer by username_1 on 2022-12-13. Score: 1

There is a major issue about the json format required, however just converting to json is easy because iterrows has to be pandas and pandas has its own output json format ... For example,
dataframe.to_json('benchling.json'', orient='records', lines=True)

If that fails then,
jsonOut = dataframe.to_json(orient='records')[1:-1].replace('},{', '} {')
with open('benchling.json', 'w') as fout:
    fout.write(jsonOut)

However, I don't think it would work and at a minimum I think the headers of the csv will need to change.

The error

"nucleotide_length": "Value must be an integer"

Looking at your code ....
"nucleotide_length": {"value": j['nucl_length']}

Sure this is right?
Why not try ...
"nucleotide_length": int(j['nucl_length'])

Thats what the error seems to be saying.


---

