My Jpath in PHP
27 Oct 2010 | 1 Comment »I had a need to use xpath like paths to fetch values from json strings. Searched and couldn’t find anything decent that suited my requirement. so ended up writing a quick and dirty implementation of my own jpath. here’s the php code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | function get_jpath($jpath, $json) { $json_object = json_decode($json); $pos_of_hash = strpos($jpath, "#"); if($pos_of_hash!==false) { // the jpath would return a list $part_till_hash = substr($jpath, 0, $pos_of_hash); $part_after_hash = substr($jpath, $pos_of_hash+1, strlen($jpath) - $pos_of_hash + 1); $count=get_count_jpath($part_till_hash,$json); $list = array(); for($i=0;$i< $count;$i++) { $returned_value = get_jpath($part_till_hash.$i.$part_after_hash, $json); if(is_array($returned_value)) { foreach ($returned_value as $item) { $list[] = $item; } } else { $list[] = $returned_value; } } return $list; } else { // the jpath would return a single value / json object $nodes = explode(".", $jpath); $objString = '$json_object'; foreach($nodes as $node) { $pos_of_index_open = strpos($node,"["); if($pos_of_index_open > 0) { $pos_of_index_close = strpos($node,"]"); $node_name = substr($node, 0, $pos_of_index_open); $node_index = substr($node, $pos_of_index_open, $pos_of_index_close - $pos_of_index_open + 1); $objString.='->{\''.$node_name.'\'}'.$node_index; } else { $objString.='->{\''.$node.'\'}'; } } eval('$value = '.$objString.';'); return($value); } } function get_count_jpath($jpath, $json) { $json_object = json_decode($json); $nodes = explode(".", $jpath); $objString = '$json_object'; foreach($nodes as $node) { $pos_of_index_open = strpos($node,"["); if($pos_of_index_open!==false) { $pos_of_index_close = strpos($node,"]"); $node_name = substr($node, 0, $pos_of_index_open); $node_index = substr($node, $pos_of_index_open, $pos_of_index_close - $pos_of_index_open + 1); $objString.='->{\''.$node_name.'\'}'.$node_index; } else { $objString.='->{\''.$node.'\'}'; } } $eval_string = '$count = count('.$objString.');'; eval($eval_string); return($count); } |
it works for xpath’s like
- batters[1].batter[1].type
- topping[#].type
- batters[#].batter[#].id
Read more if you want to see examples of how to use it.
Keep reading…

why do people twitter? this question arises in me probably because i don’t get anything to twitter most of the times. i normally do it just the for sake of doing it, or because of the fact that most of the geeks do it. most of the times people twitter about some things which are so irrelevant, not useful and that sometimes is irritating. for instance if you write a crappy blog post (almost like this one) nobody is forced to read it, somebody who might want to read might read it. But when you twitter something useless you almost force the guys following you to read that useless one liner you just tweeted.







