0

I want to search inside a JSON file with the following format:

{
  "success": true,
  "msg": "",
  "obj": [
    {
      "id": 1,
      "up": 546636462,
      "down": 4172830061,
      "total": 53687091200,
      "name": "حامد ",
      "enable": true,
      "expiryTime": 1667049201686,
      "listen": "",
      "dork": 23050,
      "net": "girltik",
      "settings": "{\n \"clients\": [\n {\n \"id\": \"f8c36812-11e0-47c4-9880-2c8ff9310c49\",\n \"alterId\": 0\n }\n ],\n \"disableInsecureEncryption\": false\n}",
      "streamSettings": "{\n \"network\": \"ws\",\n \"security\": \"none\",\n \"wsSettings\": {\n \"path\": \"/\",\n \"headers\": {}\n }\n}",
      "tag": "inbound-23050",
      "sniffing": "{\n \"enabled\": true,\n \"destOverride\": [\n \"http\",\n \"tls\"\n ]\n}"
    },
    {
      "id": 2,
      "up": 25559864,
      "down": 630133850,
      "total": 5368709120,
      "remark": "احمد ",
      "enable": true,
      "expiryTime": 1667051159682,
      "listen": "",
      "dork": 36606,
      "net": "girltik",
      "settings": "{\n \"clients\": [\n {\n \"id\": \"902b0800-6bbd-4874-f7f8-980deb8d37e8\",\n \"alterId\": 0\n }\n ],\n \"disableInsecureEncryption\": false\n}",
      "streamSettings": "{\n \"network\": \"ws\",\n \"security\": \"none\",\n \"wsSettings\": {\n \"path\": \"/\",\n \"headers\": {}\n }\n}",
      "tag": "inbound-36606",
      "sniffing": "{\n \"enabled\": true,\n \"destOverride\": [\n \"http\",\n \"tls\"\n ]\n}"
    }
  ]
}

I want to filter it by word f8c36812-11e0-47c4-9880-2c8ff9310c49 in settings property, so that the result array contains only the matching objects:

{
  "success": true,
  "msg": "",
  "obj": [
    {
      "id": 1,
      "up": 546636462,
      "down": 4172830061,
      "total": 53687091200,
      "remark": "حامد پاشائی",
      "enable": true,
      "expiryTime": 1667049201686,
      "listen": "",
      "port": 23050,
      "protocol": "vmess",
      "settings": "{\n \"clients\": [\n {\n \"id\": \"f8c36812-11e0-47c4-9880-2c8ff9310c49\",\n \"alterId\": 0\n }\n ],\n \"disableInsecureEncryption\": false\n}",
      "streamSettings": "{\n \"network\": \"ws\",\n \"security\": \"none\",\n \"wsSettings\": {\n \"path\": \"/\",\n \"headers\": {}\n }\n}",
      "tag": "inbound-23050",
      "sniffing": "{\n \"enabled\": true,\n \"destOverride\": [\n \"http\",\n \"tls\"\n ]\n}"
    }
  ]
}

How can I filter an array from a JSON file using PHP?

Anna Gevel
  • 1,103
  • 1
  • 11
  • 20
  • Why has the data in the `"settings"` key been double-encoded? That's a bad idea and isn't going to help you. If you can fix that when generating this JSON, you should. – ADyson Oct 26 '22 at 14:29

2 Answers2

1

there was a syntax error with your json content structure this is correctly version json and needed script

$str = '{
  "success": true,
  "msg": "",
  "obj": [
    {
      "id": 1,
      "up": 546636462,
      "down": 4172830061,
      "total": 53687091200,
      "name": "حامد ",
      "enable": true,
      "expiryTime": 1667049201686,
      "listen": "",
      "dork": 23050,
      "net": "girltik",
      "settings": {"clients": [{ "id": "f8c36812-11e0-47c4-9880-2c8ff9310c49","alterId": 0 } ], "disableInsecureEncryption": false},
      "streamSettings": { "network": "ws", "security": "none", "wsSettings": { "path": "/", "headers": {} }},
      "tag": "inbound-23050",
      "sniffing": { "enabled": true, "destOverride": [ "http", "tls" ]}
    },
    {
      "id": 2,
      "up": 25559864,
      "down": 630133850,
      "total": 5368709120,
      "remark": "احمد ",
      "enable": true,
      "expiryTime": 1667051159682,
      "listen": "",
      "dork": 36606,
      "net": "girltik",
      "settings": { "clients": [ { "id": "902b0800-6bbd-4874-f7f8-980deb8d37e8","alterId": 0 } ], "disableInsecureEncryption": false},
      "streamSettings": { "network": "ws", "security": "none", "wsSettings": { "path": "/", "headers": {} }},
      "tag": "inbound-36606",
      "sniffing": { "enabled": true, "destOverride": [ "http", "tls" ]}
    }
  ]
}';


$key = "f8c36812-11e0-47c4-9880-2c8ff9310c49";

$json = json_decode($str,true);

foreach ($json['obj'] as $item) {
   
    foreach($item['settings']['clients'] as $setting){
            if($setting['id']==$key) {
                    print_r($item);
                }
    }
}
Ramil Huseynov
  • 350
  • 4
  • 7
0

You can use the function json_decode() (https://www.php.net/manual/fr/function.json-decode.php). You will have an array that you can explore.

With this array, you can use the function strpos() (https://www.php.net/manual/fr/function.strpos.php) on the column "setings" This function will gives you false if the word given is not present. Otherwise it will gives you his position. With this information, you will be able to know is the word is existing.

EDIT :

You can also use the function strpos() directly with the entire json.

Matancy
  • 429
  • 4
  • 5
  • `With this array, you can apply the function strpos()`...this makes no sense. strpos searches a _string_, not an array. If you're going to suggest doing a simple string search for the value, it would need to be done on the original JSON _string_, not on the decoded array. – ADyson Oct 26 '22 at 14:15
  • I don't know much php and I can't write such a code – night leader Oct 26 '22 at 14:16
  • Either that, or you'd need to suggest searching within the `obj` array in the resulting decoded data in order to find the whole item whose ID matches the value - but that would just be a duplicate of https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value and wouldn't involve str_pos at all. – ADyson Oct 26 '22 at 14:16
  • @ADyson i mean search the word into the column "settings" of the array – Matancy Oct 26 '22 at 14:19
  • @MathéoTichy Ok. You didn't explain it clearly, you can [edit] your answer if you want. But really it would be more robust to decode the settings data and look in the correct property. – ADyson Oct 26 '22 at 14:30
  • Thank you for your response But I have not worked with json and I do not understand at all. I will be very grateful if you give me the source code – night leader Oct 26 '22 at 14:44
  • @nightleader i've given the PHP doc. The examples are very good. – Matancy Oct 26 '22 at 14:45