Json is the most common format used by API providers to express their output. Json provides a very simplified and easily understandable key-value pair formatted data that can be further manipulated and used at great ease.
But sometimes it really becomes a taunting task to traverse deep so that to reach and get particular key values, particularly in case of very lengthy json outputs and thus require a significant amount of time and efforts to get the required data.
PHP JSON Removing Keys from Json Code
In this article, we are providing a PHP code that can significantly minimize the traversal efforts and help you getting the required data in no time.
Credited to Json-key-ignorer(Ignore) class, now you can ignore the keys that are not of your use and you need to traverse through the json left out after removing the unwanted keys.
Here is the code for required json ignore keys class:
Ignore.php
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<?php /** * @param $val [The json string that we need] * @param $exclude_array [The keys made of string separated by a comma] * @return $val [The json string that do not have the ignored keys anymore] */ Class Ignore { public function ignore_keys($val='', $exclude_array='') { // i.e // $exclude_array = 'message'; $exclude_starting_index = 0; $exclude_starting_length = 0; $exclude_ending_index = 0; $strip_string = ''; $val = stripslashes($val); $exclude_array = explode(',', $exclude_array); if(count($exclude_array)) { foreach ($exclude_array as $key => $exclude) { $exclude = trim($exclude); if(strlen($exclude)) { $exclude_starting_index = strpos(strtolower($val), strtolower($exclude)); if($exclude_starting_index && ($val[$exclude_starting_index - 2] == '{' || $val[$exclude_starting_index - 2] == ',') ) { // check if the value is a valid key from the json $exclude_starting_length = $exclude_starting_index + strlen($exclude); $key_semi_colon = $exclude_starting_length + 1; if($val[$key_semi_colon + 1] != '"') $is_string = false; else $is_string = true; if($is_string) { // We should handle string with commas (,) on them. We should make sure they are inside a quote $start_quote_index = strpos($val, '"', $exclude_starting_length + 1); // get the index of the first quote; $end_quote_index = strpos($val, '"', $start_quote_index + 1); if(strpos($val, ',', $end_quote_index)){ // This means we still have multiple data ahead $exclude_ending_index = strpos($val, ',', $end_quote_index); $strip_string = substr($val, $exclude_starting_index -1 , $exclude_ending_index - $exclude_starting_index + 2); } elseif(strpos($val, '"', $end_quote_index)){ // This means this key is the last key, we need to strip up to the last quote and remove the previous comma since we reached the last key of the json $exclude_ending_index = strpos($val, '"', $end_quote_index); $strip_string = substr($val, $exclude_starting_index -2 , $exclude_ending_index - $exclude_starting_index + 2); } $val = str_replace($strip_string, '', $val); } else { $exclude_ending_index = strpos($val, ',', $exclude_starting_length); $strip_string = substr($val, $exclude_starting_index -1 , $exclude_ending_index - $exclude_starting_index + 2); $val = str_replace($strip_string, '', $val); } } } } } return $val; } } ?> |
And here is the example code taking benefit of Ignore class.
Check This Also:
How to Benchmark PHP Code For Speed
Changing the Format of Date and Time in PHP
How to Create a Downloadable CSV File in PHP
example.php
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 |
<?php require_once('ignore.php'); $ignore = new Ignore; $val = '{"employees":[ {"firstName":"John", "lastName":"Doe"}, {"email":"[email protected]", "address":"64 houstaon apartments"}, {"phone":"323569844", "designation":"agent"} ]}'; echo 'original json'.'<br>'; echo $val; echo '<br><br>'; echo 'json, after removing firstname '; echo '<br>'; $exclude = "firstName"; echo $ignore->ignore_keys($val, $exclude); echo '<br><br>'; echo 'array form'; $result= $ignore->ignore_keys($val, $exclude); $result = json_decode($result); echo "<pre>"; print_r($result); ?> |
Using above code you can easily remove unwanted keys form Json String using PHP. Thank you for reading this article. Please share if you like it.