Get Field ID Value from Json Payload using Java

Suraj Prasad :

I am trying to get the field_id for the below payload however I am stuck with the below code. I need the field_id value "70ce3705-0191-4ba4-a635-65108f1c3dde"

    JSONParser parser = new JSONParser();
    JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(jsonfilepath));
    JSONObject valid = (JSONObject) jsonObject.get("valid");
    JSONArray applications = (JSONArray) jsonObject.get("applications_added");

Payload:

{
    "valid": {
  "applications_added": [
    {
          "id": "f8ca0bda-a10c-4e8d-9c1b-6bfd24e93395",
      "status": "active",
      "custom_fields": [
          {
          "field_id": "70ce3705-0191-4ba4-a635-65108f1c3dde",
          "option_id": "db7bb12d-a26f-413e-b9e5-79af8c28b1e7"
        }

      ]
    }
  ]
}
    }
Gevorg Harutyunyan :

In your code instead of jsonObject.get("applications_added") you sould write valid.get("applications_added")

The complete solution for your problem.

    JSONParser parser = new JSONParser();
    JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(jsonfilepath));
    JSONObject valid = (JSONObject) jsonObject.get("valid");
    JSONArray applications = (JSONArray) valid.get("applications_added");
    JSONObject application  = (JSONObject)applications.get(0);
    JSONArray custom_fields = (JSONArray) application.get("custom_fields");
    JSONObject custom_field  = (JSONObject)custom_fields.get(0);
    String field_id = (String)custom_field.get("field_id");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349257&siteId=1