-1

I have a JSON file received from server, it can't be changed in any point. I'm trying to get out all key:value pairs using PHP, but my success is partial.

Did anybody deal with this kind of trouble because I couldn't find any answer yet?

I did some coding as you can see

<?php

$content = ('{
    "app_id": "test1",
    "dev_id": "temperature",
    "hardware_serial": "0056748F03A5C6CB1",
    "port": 1,
    "counter": 2217,
    "payload_raw": "AYgAAAAAAAAAAAA=",
    "payload_fields": {
        "height": 0,
        "pressure": 0,
        "temperature": 3.92
    },
    "metadata": {
        "time": "2019-07-16T20:39:37.761000811Z",
        "frequency": 867.9,
        "modulation": "LORA",
        "data_rate": "SF7BW125",
        "coding_rate": "4/5",
        "gateways": [
            {
                "gtw_id": "eui-b346ebfffec332dc",
                "timestamp": 2514820708,
                "time": "2019-07-16T20:39:37.735765Z",
                "channel": 7,
                "rssi": -65,
                "snr": 9.5,
                "rf_chain": 0,
                "latitude": 44.754036,
                "longitude": 19.690582,
                "location_source": "registry"
            }
        ]
    },
    "downlink_url": "https://integrations.net"}');


$mypayload = json_decode($content, true);


//decode and save JSON data

$AppID               = $mypayload['app_id'];
$DevID               = $mypayload['dev_id'];
$HardwareSerial      = $mypayload['hardware_serial'];
$Counter             = $mypayload['counter'];
$PayloadRaw          = $mypayload['payload_raw'];
$PayloadFields       = $mypayload['payload_field'];
$Time                = $mypayload['metadata']['time'];
$DataRate            = $mypayload['metadata']['data_rate'];
$CodingRate          = $mypayload['metadata']['coding_rate'];
$GtwID               = $mypayload['gtw_id'];
$GwTime              = $mypayload['gateways']['time'];
$GwRssi              = $mypayload['gateways']['rssi'];
$GwSnr               = $mypayload['gateways']['snr'];
$GwLatitude          = $mypayload['gateways']['latitude'];
$GwLongitude         = $mypayload['gateways']['longitude'];


$myfile     = fopen("C:/Users/Public/Documents/test2.txt","a+") or die("Unable to open file!");

fwrite($myfile, "App ID = " . $AppID . "\n" );
fwrite($myfile, "DevID = " . $DevID . "\n" );
fwrite($myfile, "HardwareSerial = " . $HardwareSerial . "\n" );
fwrite($myfile, "Counter = " . $Counter . "\n" );
fwrite($myfile, "PayloadRaw = " . $PayloadRaw . "\n" );
fwrite($myfile, "PayloadFields = " . $PayloadFields . "\n" );
fwrite($myfile, "Time    = " . $Time . "\n");
fwrite($myfile, "DataRate = " . $DataRate . "\n" );
fwrite($myfile, "CodingRate = " . $CodingRate . "\n" );
fwrite($myfile, "GtwID = " . $GtwID . "\n");
fwrite($myfile, "GwTime = " . $GwTime . "\n");
fwrite($myfile, "GwRssi = " . $GwRssi . "\n");
fwrite($myfile, "GwSnr = " . $GwSnr . "\n");
fwrite($myfile, "GwLatitude = " . $GwLatitude . "\n" );
fwrite($myfile, "GwLongitude = " . $GwLongitude . "\n" );
fwrite($myfile, "test1 = " . $test1 . "\n" );

fclose($myfile);




?>

I know why I'm getting this errors but I can't resolve it with my knowledge of PHP obviously :)

Notice: Undefined index: payload_field in E:\xampp7\htdocs\test\test.php on line 49

Notice: Undefined index: gtw_id in E:\xampp7\htdocs\test\test.php on line 53

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 54

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 55

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 56

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 57

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 58

Notice: Undefined variable: test1 in E:\xampp7\htdocs\test\test.php on line 78

So help is what I need. Thanks in advance.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
  • 1
    You need to track the path to your 'end-points', so to speak, e.g. `$mypayload['gtw_id'];` should be: `$mypayload['metadata']['gateways']['gtw_id'];` Same logic for other 'end points' you might need. You also have a typo `$mypayload['payload_field'];` should be `$mypayload['payload_fields'];` – lovelace Jul 16 '19 at 21:23

2 Answers2

1

There are several errors:

  1. the index has to be payload_fields instead of payload_field
  2. it has to be $mypayload["metadata"]["gateways"]["gtw_id"] instead of $mypayload["gtw_id"]
  3. it has to be $mypayload["metadata"]["gateways"] instead of $mypayload["gateways"]

and so on.

please check your json again.

rmac38
  • 123
  • 7
  • 1
    You have a small typo... _instead of_ `$mypayload["qtw_id"]` should be instead of `$mypayload["gtw_id"]` – lovelace Jul 16 '19 at 21:31
  • Thanks for your help. Those were the days before my vacation so I was so overwhelmed and I didn't saw obvious including typos :) – Dejan Petrovic Jul 31 '19 at 08:01
0

Covert json to array only json_decode($json_data, TRUE); than work with http://php.net/array_walk_recursive to flat array in to one dimensional array.

How to Flatten a Multidimensional Array?

From that point on it should be pretty strait forward.

Hope this helps.

Milorad
  • 21
  • 3