GSON error while getting Position.

pd95
Getting below exception while parsing JSON reponse for position.
Have printed the repsonse after stacktrace. Column 276 is at InstrumentToken ends

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was NUMBER at line 1 column 276 path $[0].instrument_token
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
at com.google.gson.Gson.fromJson(Gson.java:879)
at com.google.gson.Gson.fromJson(Gson.java:844)
at com.google.gson.Gson.fromJson(Gson.java:793)
at com.google.gson.Gson.fromJson(Gson.java:765)
at com.zerodhatech.kiteconnect.KiteConnect.getPositions(KiteConnect.java:439)

[{"day_sell_value":119018,"sell_price":297.545,"buy_value":0,"close_price":0,"tradingsymbol":"BANKNIFTY18DEC27000CE","day_buy_value":0,"value":119018,"sell_m2m":119018,"product":"NRML","sell_value":119018,"quantity":-400,"realised":0,"multiplier":1,"instrument_token":9763330,"average_price":297.545,"day_sell_price":297.545,"m2m":-2422.0000000000146,"day_buy_price":0,"day_sell_quantity":400,"sell_quantity":400,"pnl":-2422.0000000000146,"buy_quantity":0,"buy_m2m":0,"overnight_quantity":0,"exchange":"NFO","buy_price":0,"day_buy_quantity":0,"unrealised":-2422.0000000000146,"last_price":303.6}]
  • sujith
    Can you paste your code here?
  • pd95
    This is from the official example code
    public void getPositions(KiteConnect kiteConnect) throws KiteException, IOException {
    // Get positions returns position model which contains list of positions.
    Map> position = kiteConnect.getPositions();
    System.out.println(position.get("net").size());
    System.out.println(position.get("day").size());
    }

    I have simply added systrace to below Kiteconnect code in order to debug

    public Map> getPositions() throws KiteException, JSONException, IOException {
    Map> positionsMap = new HashMap<>();
    JSONObject response = new KiteRequestHandler(proxy).getRequest(routes.get("portfolio.positions"), apiKey, accessToken);
    JSONObject allPositions = response.getJSONObject("data");
    System.out.println(String.valueOf(allPositions.get("net")));
    positionsMap.put("net", Arrays.asList(gson.fromJson(String.valueOf(allPositions.get("net")), Position[].class)));
    positionsMap.put("day", Arrays.asList(gson.fromJson(String.valueOf(allPositions.get("day")), Position[].class)));
    return positionsMap;
    }
  • sujith
    Can you remove that line and try?
  • pd95
    I added that line because I was getting the error.
    After removal also, getting the error today too.
    Don't get it if zero position.
    But get it if I buy or sell option or buy a stock.
  • sujith
    The javakiteconnect is being used on many production systems by many users. We haven't observed any edge case or issue in the getPositions API.
    I would suggest re-evaluating your code or start afresh if possible.
  • pd95
    Sure, will start afresh in a new environment. Would get back if still face the issue . Thanks.
Sign In or Register to comment.