<p>As it turns out, the library I am using does not like JSON responses that contain both the field "error" and "response", even if the "error" field is set to null. I have already notified the creator of the library to possibly fix that issue and I have patched it myself as well:</p>

<p>In the <code>private Map&lt;String,Object&gt; parseJSONObject(final String jsonString)</code> function, one needs to add this at the end:</p>

<pre><code>    Map&lt;String,Object&gt; answer = (Map&lt;String,Object&gt;)json;

    if (answer.containsKey("error")){
        if (answer.get("error")==null){
            answer.remove("error");
        }
    }
    if (answer.containsKey("result")){
        if (answer.get("result")==null){
            answer.remove("result");
        }
    }

    return answer;
</code></pre>

CC BY-SA 3.0
2013-02-10T09:02:35.627Z
2013-02-10T09:02:35.627Z
ThePiachu
<p>I am starting to use a basic implementation of Bitcoin JSON RPC (<a href="http://software.dzhuvinov.com/json-rpc-2.0.html" rel="nofollow">dzhuvinov library</a>), and I am getting a strange result. Here is my code:</p>

<pre><code>public static void main(String[] args)
{
    URL serverURL = null;

    final String rpcuser ="user";
      final String rpcpassword ="pass";

      Authenticator.setDefault(new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
          }
      });
    try {
        serverURL = new URL("http://127.0.0.1:18332/");

    } catch (MalformedURLException e) {
        System.err.println(e.getMessage());
        return;
    }
     JSONRPC2Session mySession = new JSONRPC2Session(serverURL);
     String method = "getinfo";
     int requestID = 0;
     JSONRPC2Request request = new JSONRPC2Request(method, requestID);
     JSONRPC2Response response = null;
     try {
             response = mySession.send(request);

     } catch (JSONRPC2SessionException e) {
             System.err.println(e.getMessage());
             return;
     }
     if (response.indicatesSuccess())
        System.out.println(response.getResult());
    else
        System.out.println(response.getError().getMessage());
}
</code></pre>

<p>And the response I am getting is:</p>

<pre><code>Invalid JSON-RPC 2.0 response
</code></pre>

<p>Just to note - when executing this Python script:</p>

<pre><code>access = jsonrpc.ServiceProxy("http://user:pass@127.0.0.1:18332/")
print access.getinfo()
</code></pre>

<p>I get a proper results.</p>

<p>What might be causing this problem and how should I go about fixing it?</p>

CC BY-SA 3.0
2013-02-10T00:03:42.850Z
Could you attach a packet sniffer to both of them?
Nick ODell
CC BY-SA 3.0
2013-02-10T08:49:33.490Z
@NickODell Tried to, but failed. Although I figured out what the problem is some other way.
ThePiachu
<bitcoind><json-rpc><java><error>
Bitcoind returning "Invalid JSON-RPC 2.0 response"?
ThePiachu
# Bitcoind returning "Invalid JSON-RPC 2.0 response"?

(Asked by: username_0 on 2013-02-09)

I am starting to use a basic implementation of Bitcoin JSON RPC ([dzhuvinov library](http://software.dzhuvinov.com/json-rpc-2.0.html)), and I am getting a strange result. Here is my code:

```
public static void main(String[] args)
{
    URL serverURL = null;

    final String rpcuser ="user";
      final String rpcpassword ="pass";

      Authenticator.setDefault(new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
          }
      });
    try {
        serverURL = new URL("http://127.0.0.1:18332/");

    } catch (MalformedURLException e) {
        System.err.println(e.getMessage());
        return;
    }
     JSONRPC2Session mySession = new JSONRPC2Session(serverURL);
     String method = "getinfo";
     int requestID = 0;
     JSONRPC2Request request = new JSONRPC2Request(method, requestID);
     JSONRPC2Response response = null;
     try {
             response = mySession.send(request);

     } catch (JSONRPC2SessionException e) {
             System.err.println(e.getMessage());
             return;
     }
     if (response.indicatesSuccess())
        System.out.println(response.getResult());
    else
        System.out.println(response.getError().getMessage());
}

```

And the response I am getting is:

```
Invalid JSON-RPC 2.0 response

```

Just to note - when executing this Python script:

```
access = jsonrpc.ServiceProxy("http://user:pass@127.0.0.1:18332/")
print access.getinfo()

```

I get a proper results.

What might be causing this problem and how should I go about fixing it?

**Tags:** bitcoind, json-rpc, java, error

> **Comments:**
> **username_1**: Could you attach a packet sniffer to both of them?  
> **username_0**: @username_1 Tried to, but failed. Although I figured out what the problem is some other way.  

---

## 1 answers

### Answer by username_0 on 2013-02-10. Score: 1

As it turns out, the library I am using does not like JSON responses that contain both the field "error" and "response", even if the "error" field is set to null. I have already notified the creator of the library to possibly fix that issue and I have patched it myself as well:

In the `private Map<String,Object> parseJSONObject(final String jsonString)` function, one needs to add this at the end:

```
    Map<String,Object> answer = (Map<String,Object>)json;

    if (answer.containsKey("error")){
        if (answer.get("error")==null){
            answer.remove("error");
        }
    }
    if (answer.containsKey("result")){
        if (answer.get("result")==null){
            answer.remove("result");
        }
    }

    return answer;

```


---

