0

I am currently having trouble trying to parse this VCAP_SERVICES to java objects. I do not quite understand how to structure the POJO to allow it to map the values from the json string. Can someone please help me structure my pojo so that it is aligns with the json string?

I want to create objects for both of the credentials: accessToken... jdbcurl.

VCAP_SERVICES

   "VCAP_SERVICES": {
      "user-provided": [
        {
          "credentials": {
            "accessTokenUri": "tokenurl",
            "apiUrl": "apiurl",
            "clientId": "typeofID",
            "clientSecret": "secretAf",
            "scope": "none"
          },
          "syslog_drain_url": "",
          "volume_mounts": [],
          "label": "user-provided",
          "name": "OAuth2",
          "tags": []
        },
        {
          "credentials": {
            "jdbcUrl": "jdbc:oracle:connection[host]:[port]/service",
            "spring.datasource.driver-class-name": "oracle.jdbc.OracleDriver",
            "spring.datasource.initialize": "false"
          },
          "syslog_drain_url": "",
          "volume_mounts": [],
          "label": "user-provided",
          "name": "Database",
          "tags": []
        }
      ]

Java Class

ObjectMapper mapper = new ObjectMapper();
        //json String to Object

CupsProperties properties = mapper.readValue(VCAP_Services, CupsProperties.class);
        
System.out.println(properties.getJdbcUrl() + "!!!!!!!!!!!!!!!!!!!");

POJOS

public class UserProviderWrapper {
    
    @JsonProperty("user-provided")
    public List<CupsProperties> cupsProperties;
    @JsonProperty("syslog_drain_url")
    public String syslog_drain_url;
    @JsonProperty("volume_mounts")
    public List<String> volume_mounts;
    @JsonProperty("label")
    public String label;
    @JsonProperty("name")
    public String name;
    @JsonProperty("tags")
    public List<String> tags;
      //getters and setters


public class CupsProperties {
    
    @JsonProperty("jdbcUrl")
    public String jdbcUrl;
    @JsonProperty("spring.datasource.driver-class-name")
    public String driver;
    @JsonProperty("spring.datasource.initialize")
    public String initialize;
   //getters and setters

Error

Unrecognized field "user-provided" (class rest.springframework.model.CupsProperties), not marked as ignorable (2 known properties: "jdbcUrl", "dataSource"]) at [Source: {"user-provided":[{ "credentials": { "jdbcUrl": "jdbc:oracle:thin:user/pass//host:port/service", "spring.datasource.driver-class-name": "oracle.jdbc.OracleDriver", "spring.datasource.initialize": "false" }, "syslog_drain_url": "", "volume_mounts": [ ], "label": "user-provided", "name": "Oracle", "tags": [ ] }]}; line: 1, column: 19] (through reference chain: rest.springframework.model.CupsProperties["user-provided"])

6
  • JsonProperty: baeldung.com/jackson-annotations#highlighter_520754 Commented May 1, 2017 at 19:45
  • You wanna just parse certain fields out of the JSON or convert the whole JSON to mirror Java hierarchy objects? Commented May 1, 2017 at 20:27
  • @yogidilip I personally just want certain fields out of the json. I've been attempting, and following guides. I just want the values inside "Credentials" Commented May 1, 2017 at 20:28
  • In that case your POJO doesn't have to follow a certain hierarchy/structure. You can parse JSON manually and set only the fields you are interested in. Follow this example to parse examples.javacodegeeks.com/core-java/json/… Commented May 1, 2017 at 20:33
  • Thank you! The heirarchy was very confusing. Another question. if i add another service I would have two field named "credentials" how would that work? Updated post. Commented May 1, 2017 at 20:35

1 Answer 1

1

Check below solution and see if it fulfills your need. You can build on to it if you need to parse more fields.

import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonParser {

    public static void main(String[] args) {

        String VCAP_Services = "{\"userProvided\": [{\"credentials\": {\"accessTokenUri\": \"tokenurl\",\"apiUrl\": \"apiurl\",\"clientId\": \"typeofID\",\"clientSecret\": \"secretAf\",\"scope\": \"none\"},\"syslog_drain_url\": \"\",\"volume_mounts\": [],\"label\": \"user-provided\",\"name\": \"OAuth2\",\"tags\": []},{\"credentials\": {\"jdbcUrl\": \"jdbc:oracle:connection[host]:[port]/service\",\"spring.datasource.driver-class-name\": \"oracle.jdbc.OracleDriver\",\"spring.datasource.initialize\": \"false\"},\"syslog_drain_url\": \"\",\"volume_mounts\": [],\"label\": \"user-provided\",\"name\": \"Database\",\"tags\": [] } ] } ";

        CupsProperties properties=null;
        try {

            JSONParser jsonParser = new JSONParser();
            JSONObject vcapServiceJSONObject = (JSONObject) jsonParser.parse(VCAP_Services);

            for(Object key: vcapServiceJSONObject.keySet()){
                String keyStr = (String) key;
                JSONArray userProvidedList = (JSONArray) vcapServiceJSONObject.get(keyStr);

                Iterator i = userProvidedList.iterator();
                while (i.hasNext()) {
                    JSONObject innerObj = (JSONObject) i.next();
                    JSONObject credentialsObject = (JSONObject) innerObj.get("credentials");
                    if(credentialsObject.containsKey("jdbcUrl")){
                        //set to your pojo objects
                        System.out.println("JDBC url:" + credentialsObject.get("jdbcUrl"));
                    }

                    if(credentialsObject.containsKey("accessTokenUri")){
                        //set to your pojo objects
                        System.out.println("Access token URI:" + credentialsObject.get("accessTokenUri"));
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}

Output

Access token URI:tokenurl
JDBC url:jdbc:oracle:connection[host]:[port]/service
Sign up to request clarification or add additional context in comments.

3 Comments

For some reason Access token is not showing up. Not sure if cloud foundry logs messing up. JDBC url is displaying correctly.
Check your input logs to see accessToken is there and is in the right place.
Got it, it was cloud foundry logs. Thank you so much

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.