Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find JSON value based on Max date in another field

I have a JSON response body that looks like this:

[
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "PT1011",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2022-10-14T20:10:42.8276862+00:00"
        
    },
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "JD2994",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2023-03-31T16:23:19.5981913+00:00"
    }
]

I want to loop through the response body in Postman, find the MAX CreatedDate and store the ExperimentId associated with it, in a variable. So in the example above I’d want to store JD2994 in a variable.

I started with this, but am getting a max is not defined error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var jsonData = JSON.parse(responseBody)

for (var i=0, len = jsonData.length; i<len; i++) {
  var value = max(jsonData[i]["CreatedDate"]);
}

>Solution :

Here is a better loop

let maxDate = 0;
let maxId = "";
data.forEach(({CreatedDate,ExperimentId}) => {
  const d = new Date(CreatedDate).getTime();
  if (d > maxDate) {
    maxId = ExperimentId;
    maxDate = d;
  }
})
console.log(new Date(maxDate), maxId)
<script>
data = [
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "PT1011",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2022-10-14T20:10:42.8276862+00:00"
        
    },
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "JD2994",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2023-03-31T16:23:19.5981913+00:00"
    }
]</script>

Reduce:

const maxId = data.reduce((acc,{CreatedDate,ExperimentId}) => {
  if (CreatedDate>acc.date) { // the date format is string comparable
    acc.date=CreatedDate;
    acc.id=ExperimentId;
  }  
  return acc;
},{date:"",id:""}); // acc is an initialised object
console.log(maxId);
<script>
data = [
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "PT1011",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2022-10-14T20:10:42.8276862+00:00"
        
    },
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "JD2994",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2023-03-31T16:23:19.5981913+00:00"
    }
]</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading