-2

Below Json I want to get only first mail value (abc@test.com) using java. Please anybody can suggest how do I extract exact value from below Json. Thanks in advance.

{
  "profiles": [
    {
      "userId": "1234",
      "content": {
        "address": {
          "business": {
            "country": "IN",
            "locality": "Chennai",
          }
        },
        "name": {
          "first": "abc",
          "last": "abc"
        },
        "mail": [
          "abc@test.com",
          "xyz@test.com"
        ]
      }
    }
  ]
}
buhtz
  • 10,774
  • 18
  • 76
  • 149
Mohan
  • 145
  • 2
  • 10
  • 1
    You may need to verify your requirements. JSON does not impose an ordering to its records. Therefore, the email address you're looking for would not always be the first one, unless you can ensure that it always would be the first one. – dan Jul 28 '22 at 12:15
  • store your json in an variable like JSONObject obj = _your data_ . Then you can access the first mail value via obj[0].mail – Stormtrooper CWR Jul 28 '22 at 12:16
  • 4
    Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – f1sh Jul 28 '22 at 12:19
  • Parse your JSON and traverse it to location you want. There are many JSON libraries for Java, if you want more specific solution you will need to specify which library you want to use. – Pshemo Jul 28 '22 at 12:31
  • 1
    use gson or jackson – Abdelkader Chhaibi Jul 28 '22 at 12:45
  • You can also use [JsonPath](https://github.com/json-path/JsonPath). With it your code can look like `String email = JsonPath.read(jsonStr, "$.profiles[0].content.mail[0]");` – Pshemo Jul 28 '22 at 13:03

2 Answers2

1
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class Test {
    public static void main(String[] args) throws JSONException {
        String jsonstring = "{\"profiles\":[{\"userId\":\"1234\",\"content\":{\"address\":{\"business\":{\"country\":\"IN\",\"locality\":\"Chennai\"}},\"name\":{\"first\":\"abc\",\"last\":\"abc\"},\"mail\":[\"abc@test.com\",\"xyz@test.com\"]}}]}";
        System.out.println(jsonstring);
        JSONObject jsonObject = new JSONObject(jsonstring);
        JSONArray profiles = jsonObject.getJSONArray("profiles");
        for (int i = 0; i < profiles.length(); i++) {
            JSONObject curProfile= (JSONObject) profiles.get(i);
            JSONArray mails= (JSONArray)((JSONObject)curProfile.get("content")).get("mail");
            System.out.println(mails.get(0));
        }
    }
}

as they have already said json standard doesn't impose ordering, so be careful

Vavaste
  • 139
  • 1
  • 5
0

I can able to get mail by using below code

//root = my json
JsonObject rootobj = root.getAsJsonObject();
JsonArray profilesItems = rootobj.getAsJsonArray("profiles");
JsonObject firstValue = profilesItems.get(0).getAsJsonObject().getAsJsonObject("content");
JsonElement emails = firstValue.get("mail");
String mail = emails.getAsJsonArray().get(0).getAsString();
Mohan
  • 145
  • 2
  • 10