0

I am new to Java and below is the requirement, I have a JSONArray like :

[{"candidate_id":"agent_4","cluster_ID":"To_Be_Added","count":"2","utterance":"Can I text you a confirmation code on the line ending in 1544"}, {"candidate_id":"agent_11","cluster_ID":"To_Be_Added","count":"2","utterance":"Can I text you a confirmation code on the line ending in 3544"}, {"candidate_id":"agent_16","cluster_ID":"To_Be_Added","count":"63","utterance":"Hello Janet can you confirm your phone number?"}

The requirement is to extract keys from this JSONArray one by one and assign to to a JSONObject like :

{
"candidate_id": "agent_11",
"cluster_ID":"To_Be_Added",
"count":"63",
"utterance":"Can I text you a confirmation code on the line ending in 1544"

} and the rest as well as separate

Now, as of now I converted this JSONArray to list of Strings in which i will traverse and extract it one by one :

 for(int j=0;j<s.length(); j++) {
                if(s.contains("candidate_id")){
                    final String candidate_id =exampleList.get(j);
                    logger.info("print  candidate_id={}", candidate_id);
                   
                }

but it's not extracting the required results, what can be done here?

Final Output should look like :

Recommendation :{
{candidate_id: "agent_11",
cluster_ID":"To_Be_Added",
count":"63",
"utterance":"Can I text you a confirmation code on the line ending in 1544"
},
{candidate_id: "agent_13",
cluster_ID":"To_Be_Added",
count":"45",
"utterance":"Can I text you a confirmation code on the line ending in 1544"
}}
nikom
  • 71
  • 1
  • 1
  • 10
  • 1
    why do you do it by hand? you should use a library for json serialization/deserialization like jackson or gson – Pavel Jan 25 '23 at 14:16

1 Answers1

0

I believe you have a getJSONObject() method in JSONArray

JSONArray array = new JSONArray()
JSONObject object = array.getJSONObject(0)

You can just iterate over your JSONArray and create JSONObjects

UPDATE

Add this to pom.xml

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20201115</version>
</dependency>
VIVEK JAIN
  • 96
  • 12
  • In order to use getJSONObject , do i have to add some dependency to pom? – nikom Jan 25 '23 at 14:25
  • @Kadev yes, i'll update my answer to include the dependency as well – VIVEK JAIN Jan 25 '23 at 15:10
  • Thanks, but when i do this and add the data to a grpc object, it is overriding the previous values: Object object = recommendationCandidate.get("candidate_id"); Object object1 = recommendationCandidate.get("utterance"); abc.put("candidate_id", object); abc.put("cluster_id", object); // logger.debug("print abc{}", abc); eventBuilder.addAllRecommendationCandidates(abc.values()); --overriding – nikom Jan 25 '23 at 16:12
  • @kadev not sure if this is a typo but you are putting `object` inside both `candidate_id` and `cluster_id` of `abc` – VIVEK JAIN Jan 27 '23 at 04:00
  • Yes it was a typo, the things is I want to add these objects into GRPC object called eventBuilder which will carry the response as : recommendationCandidates: [{candidate_id:"abc", text : " abc", count : 2},,,and so on ] , but currently is it being overridden – nikom Jan 30 '23 at 09:15