Home > other >  How do you index two value in JSON arrays?
How do you index two value in JSON arrays?

Time:05-19

I am trying to find an array index of two matching values. I have my Groovy script below that is giving me the index of WhenWeighed and that works. Returns the correct Index. The part I am having difficulty figuring out is adding OpSeq to the indexing criteria.

What I'm trying to do is find the index of WhenWeighed and OpSeq. For Example, I want to find the index of WhenWeighed = BH and OpSeq = 30. In my below JSON this should be 4.

Can anyone help explain how you do this in Groovy?

JSON Used:

{
  "BusinessUnit": "1111111",
  "WorkOrder": 1111111,
  "WeightEstimatesInq": [
    {
      "WhenWeighed": "BH",
      "WhenWeighedDesc": "Before Heading Weight",
      "TotalWeight": 900,
      "Weight": 12,
      "OpSeq": "10",
      "AdditionalNotes": " ",
      "TareWeight": " ",
      "Effective Date": "null"
    },
    {
      "WhenWeighed": "AH",
      "WhenWeighedDesc": "After Heading Weight",
      "TotalWeight": 987,
      "Weight": 900,
      "OpSeq": "10",
      "AdditionalNotes": "Weighed Bin 10 5/17/2022",
      "TareWeight": "87",
      "Effective Date": "null"
    },
    {
      "WhenWeighed": "BO",
      "WhenWeighedDesc": "Before OSP Weight",
      "TotalWeight": 900,
      "Weight": 9,
      "OpSeq": "50",
      "AdditionalNotes": " ",
      "TareWeight": " ",
      "Effective Date": "null"
    },
    {
      "WhenWeighed": "AO",
      "WhenWeighedDesc": "After OSP Weight",
      "TotalWeight": 1000,
      "Weight": 750,
      "OpSeq": "50",
      "AdditionalNotes": " ",
      "TareWeight": "150",
      "Effective Date": "null"
    },
    {
      "WhenWeighed": "BH",
      "WhenWeighedDesc": "Before Heading Weight",
      "TotalWeight": 720,
      "Weight": 700,
      "OpSeq": "30",
      "AdditionalNotes": "Weighed Bin 30 5/17/2022",
      "TareWeight": "20",
      "Effective Date": "null"
    }
  ],
  "status": "SUCCESS",
  "startTimestamp": "2022-05-17T12:27:49.302-0400",
  "endTimestamp": "2022-05-17T12:27:50.279-0400",
  "serverExecutionSeconds": 0.977
}

Groovy Used:

  // Read Input Values
  String aWhenWeighedUDC = aInputMap.WhenWeighedUDC ?: " "
  String aInputJson = aInputMap.InputJson ?: "{}"
   
  // Initialize Output Values
  def error = " "
  def rowNumber = 0
  def lastRowNumber = 1
  // Parse JSON
  def json = new JsonSlurper().parseText( aInputJson )
    
  // Determine Row Numbers
  def rowset = json?.WeightEstimatesInq
  if ( rowset ) {
    rowNumber = rowset*.WhenWeighed.indexOf( aWhenWeighedUDC )   1
    lastRowNumber = rowset.size()
  }

CodePudding user response:

If you know that WeightEstimatesInq is always going to be the key for the list of items, you can do something like this:

json["WeightEstimatesInq"].findIndexOf {
   it["WhenWeighed"] == "BH" && it["OpSeq"] == "30"
}

which will yield 4. You can add more criteria by && it.

Note that this has the potential to return -1 if nothing matches your criteria.

  • Related