-1

i want to know json particular key has value or not

please check my below json :

[{"highest_education":"B.E ( Automobile Engineering )","occupation":"Job in Private Office","annual_income":""}]

in this json i want to check annual_income has value or not in PHP

i dont want json_decode() and check every value it is not good solution

Sword PHP
  • 23
  • 7
  • 1
    just parse it with json_decode() and check if the parsed array has key that you need. – Dawid Zbiński Aug 16 '18 at 04:51
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Aug 16 '18 at 05:11
  • my question is particular key has value or not in my json? it is not good way to json_decode() and check every key it is to much lengthy and make execution performance speed decrease. – Sword PHP Aug 16 '18 at 05:46
  • Why do you think `json_decode()` is not a good solution? It's efficient and much more robust than `strpos()`. – Raptor Aug 16 '18 at 06:13
  • because i have many json and many keys like this so it is not suitable to every json_decode and check individual key. but now it is ok now i had done. thanks you – Sword PHP Aug 16 '18 at 06:25

4 Answers4

0

First of decode the json after decoding you will get array then check particular value with condition

 json_decode($myObj);
0

To check annual_income has value or not. You have to first convert your json data into array. like below code.

$yourArray = json_encode($yourArray,true);

once your json will convert into array than u will check annual_income has value or not.

if(!empty($yourArray[0]['annual_income']) && $yourArray[0]['annual_income'] != ''){
  // Not empty
}
TrickStar
  • 229
  • 4
  • 19
  • my question is particular key has value or not in my json? it is not good way to json_decode() and check every key it is to much lengthy and make execution performance speed decrease. – Sword PHP Aug 16 '18 at 05:47
0

Well, I disagree that json_decode() has poor performance; I'll show you the proof below.

To achieve your task, you can use the following codes:

$json_string = '[{"highest_education":"B.E ( Automobile Engineering )","occupation":"Job in Private Office","annual_income":""}]';
$json_array = json_decode($json_string, true);
if(isset($json_array['annual_income']) && !empty($json_array['annual_income'])) {
    // value is not null
} else {
    // value is null or empty
}

There is no need to iterate through the JSON array at all.

Using PHP 7.1.16, I ran both methods for 1000 times, the performance difference between strpos() and json_decode() is not significant.

json_decode() used: 0.001691s
strpos() used: 0.00033s

Full test code is here: https://pastebin.com/ZcpjydfK, you can test it on your computer.

On the other side, there is a problem using strpos() approach. There is a chance that the JSON string is malformed, or space is added between the colon :, then your strpos() checking won't work.

Raptor
  • 53,206
  • 45
  • 230
  • 366
-3

it is not good way to json_decode() and check every key it is to much lengthy and make execution speed low

i got the right answer

$annual_income1='"annual_income":""';
$annual_income2='"annual_income":null';    
if(strpos($json,$annual_income1) !== false || strpos($json,$annual_income2) !== false){ 
echo "annual_income key is empty";
}
Sword PHP
  • 23
  • 7