Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    golokeshpatra45
    @golokeshpatra45

    This is my JSON -
    [{
    "app_site": "eventsubscribers",
    "app_branch": "master",
    "app_dbhash": false,
    "app_env": "production",
    "app_rev": "1.0.0.0",
    "app_tar": "eventsubscribers-master-1.0.0.0.tar"
    },
    {
    "app_site": "subscribers",
    "app_branch": "master",
    "app_dbhash": false,
    "app_env": "production",
    "app_rev": "1.0.0.0",
    "app_tar": "subscribers-master-1.0.0.0.tar"
    }]
    This is my controller -

    @RestController
         public class statusController {
    @RequestMapping("/test")
    public status getAllstatus() throws IOException, IllegalAccessException, Throwable, Exception{
    
        byte[] jsonData=Files.readAllBytes(Paths.get("C:/Users/patrag/workspace/com.json2rest/ver.json"));
        ObjectMapper objectMapper = new ObjectMapper();
        status stat1 = objectMapper.readValue(jsonData, status.class);
    
        return stat1;
    }

    }

    Now I want both the app_sites followed by their respective app_tar in the browser. How should I proceed ? As of now returning the whole object gives me RAW JSON data as o/p but I dont want the complete JSON, only particular fields of objects and their respective values (i.e app_site followed by the app_tar)

    Tom Marrs
    @tmarrs

    You need to filter the JSON data to reduce it just those 2 fields. Use jq to do this. Take a look at the jq Manual.

    After you get comfortable with jq, use the following query:

    Tom Marrs
    @tmarrs
    [.[] | { app_site, app_tar}]
    Then, update your code:
    1. Convert the jsonData byte array to a String so you can modify the data.
    2. Use the jackson-jq library to use jq from Java, and apply the above jq query. You'll use the String version of jsonData as input. Follow (jackson-jq Basic Example)[https://github.com/eiiches/jackson-jq#basic-example].
    3. Take the result (should be of type List<JsonNode>) and add it to Object Mapper. You'll need to figure this out yourself.
    Please let me know if this helps.