0

I have a JSON object as follows

{
    "mandator":"GB0010001",
    "debitAccount":"81884",
    "creditAccount":"82918",
    "trustedBeneficiary":"false",
    "localCurrencyAmount":35,
    "transactionReference":"omega7.1.1",
    "debitAccountASPSP":"t24",
    "currencyAmount":35,
    "executionDate":"20180102",
    "creditAccountASPSP":"t24",
    "transactionType":"Contactless payment",
    "trustedPSP":"false",
    "jsonErrorResponse":{
        "errorCount":0,
        "errors":[

        ]
    },
    "currency":"USD",
    "company":"GB0010001"
}

I need to remove "jsonErrorResponse":{"errorCount":0,"errors":[]}, I used JSONobject.remove("jsonErrorResponse").toString()

But it gave me output as {"errorCount":0,"errors":[]} instead of

{
    "mandator":"GB0010001",
    "debitAccount":"81884",
    "creditAccount":"82918",
    "trustedBeneficiary":"false",
    "localCurrencyAmount":35,
    "transactionReference":"omega7.1.1",
    "debitAccountASPSP":"t24",
    "currencyAmount":35,
    "executionDate":"20180102",
    "creditAccountASPSP":"t24",
    "transactionType":"Contactless payment",
    "trustedPSP":"false",
    "currency":"USD",
    "company":"GB0010001"
}
1
  • 2
    Take a look at the javadoc of the remove method. And then take a look at your JSONObject after you removed the "jsonErrorResponse". Commented Apr 16, 2019 at 9:27

3 Answers 3

2

JSONobject.remove("jsonErrorResponse") returns the thing that was removed. You are calling toString on the section you have just "deleted".

Simply do not chain the method calls.

JSONobject.remove("jsonErrorResponse");
String newJson = JSONobject.toString()
Sign up to request clarification or add additional context in comments.

Comments

0

The remove method most likely returns the object the was removed. You need to do toString on the object the you removed it from

Comments

0

Since you are working on JSONobject you are removing the content from that object.

You need to call toString() on the JSONobject.

String str = "{\"mandator\":\"GB0010001\",\"debitAccount\":\"81884\",\"creditAccount\":\"82918\",\"trustedBeneficiary\":\"false\",\"localCurrencyAmount\":35,\"transactionReference\":\"omega7.1.1\",\"debitAccountASPSP\":\"t24\",\"currencyAmount\":35,\"executionDate\":\"20180102\",\"creditAccountASPSP\":\"t24\",\"transactionType\":\"Contactless payment\",\"trustedPSP\":\"false\",\"jsonErrorResponse\":{\"errorCount\":0,\"errors\":[]},\"currency\":\"USD\",\"company\":\"GB0010001\"}"
JSONObject jsonObject = new JSONObject(str);
jsonObject.remove("jsonErrorResponse");
jsonObject.toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.