<p>It is possible for <code>readJSON</code> to fail due to whitespaces, leading and/or trailing. Appending <code>trim()</code> to the string could prevent this issue.</p>
<pre><code>def my_json_string = readFile(&quot;my_json_file.json&quot;).trim()
def my_json = readJSON text: my_json_string
</code></pre>

CC BY-SA 4.0
2023-01-27T14:01:43.340Z
2023-01-27T14:01:43.340Z
slybloty
<pre><code>Warning: API call to create metrics set failed:
[Pipeline] echo
net.sf.json.JSONException: Invalid JSON String
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
at org.jenkinsci.plugins.pipeline.utility.steps.json.ReadJSONStepExecution.doRun(ReadJSONStepExecution.java:86)
at org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:32)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
</code></pre>
<p>I'm basically using a script on jenkins piepline to send data to grafana
I'm facing this issue on jenkins version 2.235.1</p>
<p>The same script is working on another jenkins instance and it doesn't display this error</p>
<p>I think it's caused by plugin deprecated version or jenkins itself</p>
<p>I'm not sure if there is a workaround or which plugin to update</p>

<jenkins><jenkins-pipeline><jenkins-plugins>
net.sf.json.JSONException: Invalid JSON String error on jenkins
aymen0406
# net.sf.json.JSONException: Invalid JSON String error on jenkins

(Asked by: username_0 on 2021-09-16)

Warning: API call to create metrics set failed:
[Pipeline] echo
net.sf.json.JSONException: Invalid JSON String
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
at org.jenkinsci.plugins.pipeline.utility.steps.json.ReadJSONStepExecution.doRun(ReadJSONStepExecution.java:86)
at org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:32)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

I'm basically using a script on jenkins piepline to send data to grafana
I'm facing this issue on jenkins version 2.235.1
The same script is working on another jenkins instance and it doesn't display this error
I think it's caused by plugin deprecated version or jenkins itself
I'm not sure if there is a workaround or which plugin to update

**Tags:** jenkins, jenkins-pipeline, jenkins-plugins


---

## 1 answers

### Answer by username_1 on 2023-01-27. Score: 0

It is possible for readJSON to fail due to whitespaces, leading and/or trailing. Appending trim() to the string could prevent this issue.
def my_json_string = readFile("my_json_file.json").trim()
def my_json = readJSON text: my_json_string


---

