0

I have following Json Data in php url File name : house.php

{
"Error": "",
"ErrorCode": 0,
"Guid": "",
"Id": 0,
"Success": true,
"Value": [
{
"MAIN": 225,
"PHASE": 219418523,
"G": "7TH Division",
"GI": "2031892",
"DOOR": "8907",
"Gate": {
    "RG": 2,
    "BNS": "4 Window",
    "BS": {
    "SB1": 2
    },
    "TQ": [
    {
        "Key": 1,
        "Value": {
        "T1": 2
        }
    },
    {
        "Key": 2,
        "Value": {}
    }
    ],
    "MR": -1,
    "MS": 600
},
"AA": "81",
"AI": "8745524"
},
{
"MAIN": 300,
"PHASE": 219418523,
"G": "8TH Division",
"GI": "4526892",
"DOOR": "8877",
"GATE": {
    "RG": 2,
    "BNS": "5 Window",
    "BS": {
    "SB1": 2
    },
    "TQ": [
    {
        "Key": 1,
        "Value": {
        "T1": 2
        }
    },
    {
        "Key": 2,
        "Value": {}
    }
    ],
    "MR": -1,
    "MS": 600
},
"AA": "91",
"AI": "8758421"
}]}

I need particular data "G", "G1", "DOOR", "AA", A1" alone to display in my new php file :

completehouse.php in json format what code to give to get particular data.

    <?php
$url="house.php";
$text = file_get_contents($url);
header('Content-Type: application/json');
echo $text;
?>

Currently i am using https://codebeautify.org/online-json-editor manually each time by using transform data query : [*].{G: G, G1: G1, DOOR: DOOR, AA: AA, AI: AI} and getting output

{
      "Error": "",
      "ErrorCode": 0,
      "Guid": "",
      "Id": 0,
      "Success": true,
      "Value": [
{
          "G": "7TH DIVISION",
          "G1": "2031892",
          "DOOR": "8907",
          "AA": "81",
          "AI": "8745524"
        },
{
          "G": "8TH DIVISION",
          "G1": "4526892",
          "DOOR": "8877",
          "AA": "85",
          "AI": "8759544"
        }
      ]
    }

Please someone help me to fix my manual work and save my time.

MicroJson
  • 15
  • 6

2 Answers2

0

You can decode your json and then get the exact value of what you want.

$json = '....';
$parsed = json_decode($json,true);
$result = [];
if($parsed['Success']){ // check if your api json response is true. 
    foreach($parsed['Value'] as $val){
        if($val['AI']> = 9000000){
        $result[] = [
            "G"=> $val['G'],
            "GI"=> $val['GI'],
            "DOOR"=> $val['DOOR'],
            "AA"=> $val['AA'],
            "AI"=> $val['AI']
         ];
         }
         // or what you want.
    }
}

echo json_encode($result);

see demo

ttrasn
  • 4,322
  • 4
  • 26
  • 43
  • this json file updated frequently.so i can't use $json = '....';. i get this json file in another site url house.php. i need to get particular data and generate json file in completehouse.php in my site . what code i have to use in completehouse.php to get. is it possible ? – MicroJson Dec 30 '19 at 17:15
  • @MicroJson you can get file content of other server with `file_get_content` – ttrasn Dec 30 '19 at 17:20
  • thank you very much, you saved me, got all 5 required data, one more things to fix. i don't want to display data if AI >= 9000000 is that possible? – MicroJson Dec 30 '19 at 17:57
  • @MicroJson your welcome. you can accept my answer. to check `AI >= 9000000` you can add this condition in `foreach` – ttrasn Dec 30 '19 at 18:17
  • Accepted. can you please update code for foreach . I don't want to display data if AI > = 9000000. And also result section not displayed '{ "Error": "", "ErrorCode": 0, "Guid": "", "Id": 0, "Success": true, "Value": [' – MicroJson Dec 30 '19 at 18:42
  • @MicroJson i updated my answer. did it what you want ? – ttrasn Dec 30 '19 at 18:44
  • result section not displayed '{ "Error": "", "ErrorCode": 0, "Guid": "", "Id": 0, "Success": true, "Value": [' – MicroJson Dec 31 '19 at 04:19
  • @MicroJson where is problem ? i didnt get your issue – ttrasn Dec 31 '19 at 05:32
  • @MicroJson also why you unaccept my answer ?! :)) am i didnt help you and my answer didnt answer of your question ? – ttrasn Dec 31 '19 at 05:32
0

As you only need to manipulate the Value column so below code will keep all values same but get the specific columns from Value.

$url="house.php";
$text = file_get_contents($url);

$data = json_decode($text, true);

if ($data['Success'] === true) {
    $v = [];
    $columns = ["G", "GI", "DOOR", "AA", "AI"];
    for ($i = 0; $i< count($data['Value']); $i++) {
        foreach ($data['Value'][$i] as $key => $val) {
            if (in_array($key, $columns)) {
                $v[$key] = $val;
            }
        }
        $data['Value'][$i] = $v;
    }
}

$text = json_encode($data);