JSONItem not behaving like one

I’m struggling to understand how to get to the ‘step’ items in the JSON (below). I’ve tried parsing it into a dictionary, and all sorts of things, but I feel I’m overlooking something obvious. The KeyAt picks up the correct key of “emf_plan_steps”, but trying to take any action on this item is throwing errors. Thanks for any help.

// here’s the json_inbound…

{"emf_plan_name":"Feed Plan ABC","emf_plan_steps":[{"step_name":"STEP 1","start_rate":0.021,"end_rate":0.021,"time_span_mins":58},{"step_name":"STEP 2","start_rate":0.021,"end_rate":0.022,"time_span_mins":59}]}

Var json_inbound As New JSONItem(packet)

For i As Integer = 0 To json_inbound.Count - 1

  If json_inbound.IsArray Then
    
    // doesn't think it's an array...
    
  Else
    
    Var key As String = json_inbound.KeyAt(i)
    
    If json_inbound.value(key) IsA JSONItem Then

        // get's here just fine...
        // ...but the next line throws a TypeMismatchException

        System.DebugLog(CurrentMethodName + " " + json_inbound.value(key).StringValue)

    End If

  End If

Next

Organize it to understand:

{
  "emf_plan_name": "Feed Plan ABC",
  "emf_plan_steps": [
    {
      "step_name": "STEP 1",
      "start_rate": 0.021,
      "end_rate": 0.021,
      "time_span_mins": 58
    },
    {
      "step_name": "STEP 2",
      "start_rate": 0.021,
      "end_rate": 0.022,
      "time_span_mins": 59
    }
  ]
}

The object contains 2 properties:

emf_plan_name : string , and
emf_plan_steps : array of objects

Every emf_plan_steps(n) contains an object with 4 properties:

step_name : string ,
start_rate : double,
end_rate : double , and
time_span_mins : integer

2 Likes
Const packet = "{""emf_plan_name"":""Feed Plan ABC"",""emf_plan_steps"":[{""step_name"":""STEP 1"",""start_rate"":0.021,""end_rate"":0.021,""time_span_mins"":58},{""step_name"":""STEP 2"",""start_rate"":0.021,""end_rate"":0.022,""time_span_mins"":59}]}"

Var js As New JSONItem(packet)

System.DebugLog "emf_plan_name = "+js.Value("emf_plan_name")

Var steps As JSONItem = js.Value("emf_plan_steps")

System.DebugLog "# of Steps : "+steps.Count.ToString

For i As Integer = 0 to steps.LastRowIndex
  
  Var stepNum As Integer = i + 1
  
  System.DebugLog "Step # " + stepNum.ToString
  
  Var st As JSONItem = steps.ValueAt(i)
  
  System.DebugLog "step_name = "+st.Value("step_name")
  System.DebugLog "start_rate = "+st.Value("start_rate").DoubleValue.ToString
  System.DebugLog "end_rate = "+st.Value("end_rate").DoubleValue.ToString
  System.DebugLog "time_span_mins = "+st.Value("time_span_mins").IntegerValue.ToString
  
Next

break // check debug messages
2 Likes

Thank you Rick, you helped me see the forest thru the trees. Very much appreciated!

1 Like