Home > Back-end >  Trying to access an unpredicable JSON object name with PHP
Trying to access an unpredicable JSON object name with PHP

Time:01-09

So, I'm trying take data from a JSON file that I'm getting from a paid site that hosts fantasy football data. My goal is to take the data and put into a mysql database however, I'm having trouble figuring out how to access one specific element of the JSON data.

Shown below - there are 3 sets of stats, each under a pseudo-random "id" (2131774, 1725130, 2139141)

Here is the data as from the site (there are ~1500 records, so this is just a snippet to show the structure)

{
    "body": {
        "player_stats": {
            "2131774": {
                "RuReYd": 1,
                "TK": "0.0",
                "RuYd": "1",
                "ID": "BEC046566",
                "RuAtt": "1",
                "D": "12/18/2022",
                "TM": "DEN",
                "RuFDPct": "0.0",
                "TarOpp": 26,
                "Tar": "1",
                "G": "1",
                "PaRuYd": 1,
                "TotYd": 1,
                "OPP": "vsARI",
                "GW": "1",
                "RuLg": "1",
                "RuAvg": "1.0",
                "TarPct": "3.8"
            },
            "1725130": {
                "TK": "0.0",
                "RuReYd": 105,
                "ReAvg": "10.5",
                "ID": "KEL012458",
                "TarOpp": 41,
                "Recpt": "10",
                "ReYd": "105",
                "D": "12/18/2022",
                "TM": "KC",
                "Tar": "10",
                "ReLg": "18",
                "PenA": "1",
                "G": "1",
                "TotYd": 105,
                "ReFD": "7",
                "OPP": "atHOU",
                "ReFDPct": "70.0",
                "GW": "1",
                "GS": "1",
                "PenYdA": "11",
                "TarPct": "24.4"
            },
            "2139141": {
                "GL": "1",
                "ID": "FRA561925",
                "G": "1",
                "STK": "5",
                "TK": "6.0",
                "FF": "1",
                "TarPct": "0.0",
                "PenA": "1",
                "GS": "1",
                "OPP": "atMIN",
                "ATK": "4",
                "PenYdA": "13",
                "TM": "IND",
                "D": "12/17/2022"
            },},},}

Using PHP json_decode, I've been able to get to all of the lower objects like "RuReYd", "TK", etc...

***But I can't figure out how to get those "id's" (2131774, 1725130, 2139141)


Here's the code I wrote to get that data:

$url = "https://...";
// get json
$val = file_get_contents($url);
$decoded_json = json_decode($val);
foreach ($decoded_json->body as $obj) {
   var_dump($obj);
 //THIS IS WHERE I NEED TO GET THE pseudo-random "ID" UNDER THE "player_stats" OBJECT
   foreach ($obj as $pstats)
    {
        var_dump($pstats);
        //get data from JSON obj
        $elias_id = $pstats->ID ?? null;
        $OPP = $pstats->OPP ?? null;
        $TM = $pstats->TM ?? null;
    }

}

This var_dump of $obj looks like this:

The data below is different but in the same structure.
In the JSON, each set of stats has a unique "id" that i need pull into a variable (I'm using it as the primary key for this table).

The bold data is what I'm trying get into mysql.

class stdClass#3 (1501) {
public $**1998928** =>
  class stdClass#2 (10) {
    public $D =>
    string(10) "09/11/2022"
    public $G =>
    string(1) "1"
    public $GL =>
    string(1) "1"
    public $TM =>
    string(3) "CAR"
    public $GS =>
    string(1) "1"
    public $ATK =>
    string(1) "4"
    public $TarPct =>
    string(3) "0.0"
    public $OPP =>
    string(5) "vsCLE"
    public $ID =>
    string(9) "IOA458455"
    public $TK =>
    string(3) "0.0"
  }
  public $**1860773** =>
  class stdClass#4 (10) {
    public $D =>
    string(10) "09/12/2022"
    public $G =>
    string(1) "1"
    public $OPP =>
    string(5) "vsDEN"
    public $ID =>
    string(9) "JEF350399"
    public $GW =>
    string(1) "1"
    public $TK =>
    string(3) "1.0"
    public $TM =>
    string(3) "SEA"
    public $ATK =>
    string(1) "1"
    public $STK =>
    string(1) "1"
    public $TarPct =>
    string(3) "0.0"
  }
  public $**2866970** =>
  class stdClass#6 (8) {
    public $OPP =>
    string(5) "vsDEN"
    public $TK =>
    string(3) "0.0"
    public $GW =>
    string(1) "1"
    public $ID =>
    string(9) "BRY315524"
    public $TM =>
    string(3) "SEA"
    public $TarPct =>
    string(3) "0.0"
    public $G =>
    string(1) "1"
    public $D =>
    string(10) "09/12/2022"
  }

I've tried talking down the decoded json like this:

foreach ($decoded_json->body->player_stats as $obj)

but then I have no way to get to the nested data.

Any help would be appreciated

CodePudding user response:

Why don't you work with arrays instead of objects, like this:

<?php

$val = '{
    "body": {
        "player_stats": {
            "2131774": {
                "RuReYd": 1,
                "TK": "0.0",
                "RuYd": "1",
                "ID": "BEC046566",
                "RuAtt": "1",
                "D": "12/18/2022",
                "TM": "DEN",
                "RuFDPct": "0.0",
                "TarOpp": 26,
                "Tar": "1",
                "G": "1",
                "PaRuYd": 1,
                "TotYd": 1,
                "OPP": "vsARI",
                "GW": "1",
                "RuLg": "1",
                "RuAvg": "1.0",
                "TarPct": "3.8"
            },
            "1725130": {
                "TK": "0.0",
                "RuReYd": 105,
                "ReAvg": "10.5",
                "ID": "KEL012458",
                "TarOpp": 41,
                "Recpt": "10",
                "ReYd": "105",
                "D": "12/18/2022",
                "TM": "KC",
                "Tar": "10",
                "ReLg": "18",
                "PenA": "1",
                "G": "1",
                "TotYd": 105,
                "ReFD": "7",
                "OPP": "atHOU",
                "ReFDPct": "70.0",
                "GW": "1",
                "GS": "1",
                "PenYdA": "11",
                "TarPct": "24.4"
            },
            "2139141": {
                "GL": "1",
                "ID": "FRA561925",
                "G": "1",
                "STK": "5",
                "TK": "6.0",
                "FF": "1",
                "TarPct": "0.0",
                "PenA": "1",
                "GS": "1",
                "OPP": "atMIN",
                "ATK": "4",
                "PenYdA": "13",
                "TM": "IND",
                "D": "12/17/2022"
            }
        }
    }
}';


$decoded_json = json_decode($val, true);

foreach ($decoded_json['body']['player_stats'] as $id => $playerStats) {
    echo 'Player id is: ' . $id;
    echo ' Player OPP is: ' . $playerStats['OPP'] . PHP_EOL;
}

Output is:

Player id is: 2131774 Player OPP is: vsARI
Player id is: 1725130 Player OPP is: atHOU
Player id is: 2139141 Player OPP is: atMIN

And you could access whatever you want easily with $playerStats['whatyouwanttoaccess']

  • Related