Mike Korza Mike Korza - 3 months ago 15
Java Question

Golang/Java Failed to unmarshal JSON from middleware

I am writing a middleware for a GoLang application (http://hoverfly.io/) but having some issues.

The concept is very simple. Golang passes in a JSON input to my Java middleware, and my middleware then returns a modified json output to the Golang application.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.json.JSONObject;

public class mid
{

static String payload ="";
public static void main (String[] args) throws Exception
{
try {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);


payload = scanner.nextLine();


JSONObject obj = new JSONObject(payload);


JSONObject response = obj.getJSONObject("response");

response.remove("status");

response.put("status", "500");



String newjson = obj.toString();


write (newjson);

System.out.print(obj);


} catch (Exception e) {
e.printStackTrace();
}



}
}


I then run the program:

./hoverfly -middleware "java -jar mid.jar"


The JSON input to my program is this:

{"response":{"status":200,"body":"\u003c?xml version=\"1.0\"?\u003e\u003cpathList xmlns:xlink=\"http://www.w3.org/1999/xlink\"\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/0/\"\u003e0\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/1/\"\u003e1\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/3/\"\u003e3\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/4/\"\u003e4\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/5/\"\u003e5\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/6/\"\u003e6\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/7/\"\u003e7\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/8/\"\u003e8\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/9/\"\u003e9\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/10/\"\u003e10\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/11/\"\u003e11\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/12/\"\u003e12\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/13/\"\u003e13\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/14/\"\u003e14\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/15/\"\u003e15\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/16/\"\u003e16\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/17/\"\u003e17\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/18/\"\u003e18\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/19/\"\u003e19\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/20/\"\u003e20\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/21/\"\u003e21\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/22/\"\u003e22\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/23/\"\u003e23\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/25/\"\u003e25\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/26/\"\u003e26\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/27/\"\u003e27\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/28/\"\u003e28\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/29/\"\u003e29\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/30/\"\u003e30\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/31/\"\u003e31\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/32/\"\u003e32\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/33/\"\u003e33\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/34/\"\u003e34\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/35/\"\u003e35\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/36/\"\u003e36\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/37/\"\u003e37\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/38/\"\u003e38\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/39/\"\u003e39\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/40/\"\u003e40\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/41/\"\u003e41\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/42/\"\u003e42\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/43/\"\u003e43\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/44/\"\u003e44\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/45/\"\u003e45\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/46/\"\u003e46\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/47/\"\u003e47\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/48/\"\u003e48\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/49/\"\u003e49\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/50/\"\u003e50\u003c/path\u003e\n \u003cpath xlink:href=\"http://www.testapi.com/endpoint/path/5555/\"\u003e5555\u003c/path\u003e\n\u003c/pathList\u003e","encodedBody":false,"headers":{"Content-Length":["4580"],"Content-Type":["application/xml"],"Date":["Fri, 02 Sep 2016 11:31:17 GMT"],"Hoverfly":["Was-Here"],"Server":["Apache-Coyote/1.1"]}},"request":{"requestType":"recording","path":"/endpoint/path/","method":"GET","destination":"www.testapi.com","scheme":"http","query":"","body":"","headers":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"Accept-Encoding":["gzip, deflate"],"Accept-Language":["en-US,sv-SE;q=0.8,sv;q=0.5,en;q=0.3"],"Connection":["keep-alive"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0"]}}}


The output sent to hoverfly is then this:

{"response":{"headers":{"Hoverfly":["Was-Here"],"Date":["Fri, 02 Sep 2016 11:31:17 GMT"],"Content-Length":["4580"],"Content-Type":["application/xml"],"Server":["Apache-Coyote/1.1"]},"body":"<?xml version=\"1.0\"?><endpointList xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <endpoint xlink:href=\"http://testapi/path/endpoint/0/\">0<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/1/\">1<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/3/\">3<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/4/\">4<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/5/\">5<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/6/\">6<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/7/\">7<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/8/\">8<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/9/\">9<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/10/\">10<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/11/\">11<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/12/\">12<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/13/\">13<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/14/\">14<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/15/\">15<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/16/\">16<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/17/\">17<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/18/\">18<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/19/\">19<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/20/\">20<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/21/\">21<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/22/\">22<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/23/\">23<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/25/\">25<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/26/\">26<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/27/\">27<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/28/\">28<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/29/\">29<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/30/\">30<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/31/\">31<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/32/\">32<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/33/\">33<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/34/\">34<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/35/\">35<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/36/\">36<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/37/\">37<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/38/\">38<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/39/\">39<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/40/\">40<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/41/\">41<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/42/\">42<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/43/\">43<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/44/\">44<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/45/\">45<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/46/\">46<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/47/\">47<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/48/\">48<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/49/\">49<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/50/\">50<\/endpoint>\n <endpoint xlink:href=\"http://testapi/path/endpoint/5555/\">5555<\/endpoint>\n<\/endpointList>","status":"500","encodedBody":false},"request":{"headers":{"Accept-Language":["en-US,sv-SE;q=0.8,sv;q=0.5,en;q=0.3"],"Accept-Encoding":["gzip, deflate"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0"],"Connection":["keep-alive"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"]},"body":"","scheme":"http","query":"","path":"/path/endpoint/","method":"GET","requestType":"recording","destination":"testapi"}}


However when I run this I get the error:

"error":"json: cannot unmarshal string into Go value of type int","level":"error","msg":"Failed to unmarshal JSON


Is Go expecting some kind of special JSON format or what can the problem be?

All help is appreciated.

Answer

The error message says cannot unmarshal string into Go value of type int. Which means you are sending something as string when Go is expecting it to be an int.

The Go code is trying to unmarshall JSON using this definition

Solution:

"status":"500" should be sent as "status":500

Try

 response.put("status", 500);