0

I have a json arrary with many json messsages. I then parse the json message to process each json object. I need to figure out how to take a single json object that fails and append all the failed json objects back into another json array to create a new file. I'm not sure how to convert a json object from the parseText() method back to a normal json message or how to append objects back into a json array to create a file. Can someone help me with this?

    Main json file array

    [
      {
        "Account": "1",
        "Name":    "Test1"
      },
      {
        "Account": "2",
        "Name":    "Test2"
      },
      {
        "Account": "3",
        "Name":    "Test3"
      },
      {
        "Account": "4",
        "Name":    "Test4"
      }
    ]  

    String sJson = groovy.json.StringEscapeUtils.unescapeJava(jsonFile.toString());
    jsonResp = new groovy.json.JsonSlurper().parseText(sJson));

    for( int x=0; x < jsonResp?.size(); x++ ) {
        processJson( jsonResp[x] )
    }

    void processJson( Object jsonResp ) {


       If object message fails in this function, need to convert the json object back into a json message
       and append it to a jsonArray to create a new json file.

    }

    This is what the new json file would like if test samples 2 & 4 failed
    [
      {
        "Account": "2",
        "Name":    "Test2"
      },
      {
        "Account": "4",
        "Name":    "Test4"
      }
    ]  

1 Answer 1

2

I found the answer to my question.

JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonResp);
println jsonArray.toString()
Sign up to request clarification or add additional context in comments.

2 Comments

what is fully qualified class name of JSONArray
import org.json.simple.JSONArray;

Your Answer

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