Monday, April 4, 2016

Simple JsonPath in Python


Following python method is simplified jsonpath implementation that takes json object and jsonPath as input and returns json object at that json path as output.

For json array, we should use ".[element_num]". For example, from
    {"emp" : ["Indra", "Narada", "Yama"] }
to get "Narada", json path should be "emp.[1]". Remember, count starts with zero.

For objects with names that may vary, you may use "{object_number}". For example, from    
   {"machines": { "1a2d": { "os" : "Linux", "ram" : "16GB", "uptime" : "20045"}}}
if we need to get uptime,  json path can be "machines.{0}.uptime". But please ensure sequence of element in json is always fixed.

No comments:

Post a Comment