skip to Main Content

I’m looking for some advice to come up with a design for a use case on which I’m working. As a REST request I receive a Json payload something like below

{ 
  "marketEvent" :{
        "category": "Finance"
        "version": 1.0       
   }
   "history" : {
     "type" : "FX"
     "value": 45.33  
     "role" : " Broker"  
   } 
}

For different categories (in this example Finance), I need to access different values from the JSON. In case of Finance, I need to access history.type, but in another category I might need to access another value.

I’m looking for a generic design in Java/Spring where I can configure different categories and what values I need to access and take action on that.

2

Answers


  1. Jayway is perfect for this. Along with a map from the categories and paths to the data.

    Dependency

    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.4.0</version>
    </dependency>
    

    Code

    import java.util.HashMap;
    import java.util.Map;
    
    import com.jayway.jsonpath.DocumentContext;
    import com.jayway.jsonpath.JsonPath;
    
    public class App5 {
    
        
        public static void main(String[] args) {
            String jsonDataSourceString = "{ rn" + 
                    "  "marketEvent" :{rn" + 
                    "        "category": "Finance", rn" + 
                    "        "version": 1.0       rn" + 
                    "   }, rn" + 
                    "   "history" : {rn" + 
                    "     "type" : "FX", rn" + 
                    "     "value": 45.33,  rn" + 
                    "     "role" : " Broker"  rn" + 
                    "   } rn" + 
                    "}";
    
            System.out.println(jsonDataSourceString);
    
            Map<String, String> categoryToJsonPath = new HashMap<>();
            categoryToJsonPath.put("Finance", "$.history.type");
            
            String pathToCategory = "$.marketEvent.category";
            
            DocumentContext jsonContext = JsonPath.parse(jsonDataSourceString);
            String categoryName = jsonContext.read(pathToCategory);
            
            System.out.println(categoryName);
    
            String pathToCategoryDependentField = categoryToJsonPath.get(categoryName);
            
            String categoryDependentFieldValue = jsonContext.read(pathToCategoryDependentField);
    
            System.out.println(categoryDependentFieldValue);
            
    
        }
    
    }
    

    Output:

    { 
      "marketEvent" :{
            "category": "Finance", 
            "version": 1.0       
       }, 
       "history" : {
         "type" : "FX", 
         "value": 45.33,  
         "role" : " Broker"  
       } 
    }
    Finance
    FX
    
    Login or Signup to reply.
  2. You many try library Josson. Function caseValue() can do the job.

    https://github.com/octomix/josson

    Josson josson = Josson.fromJsonString(
        "{" +
        "  "marketEvent" :{" +
        "        "category": "Finance"," +
        "        "version": 1.0" +
        "   }," +
        "   "history" : {" +
        "     "type" : "FX"," +
        "     "value": 45.33," +
        "     "role" : " Broker"" +
        "   }" +
        "}");
    String categoryValue = josson.getString(
        "marketEvent.category.caseValue(" +
        "  'Finance', $.history.type," +
        "  'IT', $.history.type2)");
    System.out.println(categoryValue);
    // Output: FX
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search