Home > Net >  JSONPath First element of conditional match
JSONPath First element of conditional match

Time:06-03

I have a list of results, and I want to get the first value of one item when the other item is greater than a specific value. In this case the "time" when "t_evap" is greater than 881.

Similar to this question, that has no relevant answer - JsonPath: getting first element in string list

JSON

    {
        "data": [
            {
                "time":  0.00000000000000E 000,
                "p_evap":  3.78425000000000E 003,
                "t_evap":  8.80000000000000E 002,
                "a_evap":  2.15910676000000E-001,
                "p_cond":  3.78425000000000E 003,
                "t_cond":  8.80000000000000E 002,
                "a_cond":  2.15910676000000E-001
            },
            {
                "time":  1.00000000000000E 002,
                "p_evap":  3.86011552200429E 003,
                "t_evap":  8.81574884518309E 002,
                "a_evap":  2.15062064174011E-001,
                "p_cond":  3.87066987863156E 003,
                "t_cond":  8.80894133103899E 002,
                "a_cond":  2.16937100305237E-001
            },
            {
                "time":  2.00000000000000E 002,
                "p_evap":  3.91391364701754E 003,
                "t_evap":  8.82989737115102E 002,
                "a_evap":  2.11843836231414E-001,
                "p_cond":  3.95342159379127E 003,
                "t_cond":  8.81491251833285E 002,
                "a_cond":  2.20305806875845E-001
            }
        ],
        "error_count": 0,
        "error_messages": []
    }

JSONPath $.data[?(@.t_evap> 881)].time returns [100, 200] - I need to get back 100.

How do I specify the first item as $.data[?(@.t_evap> 881)].time[0] would be trying to give the first char of each item, but since it is a # it errors.

CodePudding user response:

Since json path always returns an array, you have to apply [0] to a result array only, not to a json path. this is working for me

var times = jsonPath(d,"$.data[?(@.t_evap> 881)].time"); // [100,200]

console.log( times[0] ); //100

CodePudding user response:

This doesn't seem to be currently supported according to json-path/JsonPath#272.

If you can only process using JsonPath, then applying a second query to the first result might be your best bet.

  • Related