Home > Enterprise >  search in json with php
search in json with php

Time:10-27

I want to search inside a json file

`

{
  "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 search if there is a word f8c36812-11e0-47c4-9880-2c8ff9310c49 in settings, for me only its array should be printed as follows:

{ "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}" } ] }

I want to write the php code for this please help me thanks

CodePudding user response:

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);
                }
    }
}

CodePudding user response:

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.

  • Related