Search code examples
phphtmljsonparsingjsonparser

Busy JSON file parsing in php


I am trying to extract data values from a rather large JSON file via php. The file comes from a USGS server and includes a large series of information that I don't need I am just trying to get the data values and not all of the metadata. I have never had to handle JSON files of this magnitude as I am rather new to their format.

{
"name": "ns1:timeSeriesResponseType",
"declaredType": "org.cuahsi.waterml.TimeSeriesResponseType",
"scope": "javax.xml.bind.JAXBElement$GlobalScope",
"value": {
    "queryInfo": {
        "queryURL": "http://waterservices.usgs.gov/nwis/iv/format=json&sites=01129500&startDT=2017-11-05T18:00-0500&endDT=2017-11-06T03:00-0500&parameterCd=00060&siteStatus=all",
        "criteria": {
            "locationParam": "[ALL:01129500]",
            "variableParam": "[00060]",
            "timeParam": {
                "beginDateTime": "2017-11-05T23:00:00.000",
                "endDateTime": "2017-11-06T08:00:00.000"
            },
            "parameter": []
        },
        "note": [
            {
                "value": "[ALL:01129500]",
                "title": "filter:sites"
            },
            {
                "value": "[mode=RANGE, modifiedSince=null] interval={INTERVAL[2017-11-05T23:00:00.000Z/2017-11-06T08:00:00.000Z]}",
                "title": "filter:timeRange"
            },
            {
                "value": "methodIds=[ALL]",
                "title": "filter:methodId"
            },
            {
                "value": "2017-11-06T21:03:28.078Z",
                "title": "requestDT"
            },
            {
                "value": "efbacfd0-c335-11e7-9d73-6cae8b663fb6",
                "title": "requestId"
            },
            {
                "value": "Provisional data are subject to revision. Go to http://waterdata.usgs.gov/nwis/help/?provisional for more information.",
                "title": "disclaimer"
            },
            {
                "value": "vaas01",
                "title": "server"
            }
        ]
    },
    "timeSeries": [
        {
            "sourceInfo": {
                "siteName": "CONNECTICUT RIVER AT NORTH STRATFORD, NH",
                "siteCode": [
                    {
                        "value": "01129500",
                        "network": "NWIS",
                        "agencyCode": "USGS"
                    }
                ],
                "timeZoneInfo": {
                    "defaultTimeZone": {
                        "zoneOffset": "-05:00",
                        "zoneAbbreviation": "EST"
                    },
                    "daylightSavingsTimeZone": {
                        "zoneOffset": "-04:00",
                        "zoneAbbreviation": "EDT"
                    },
                    "siteUsesDaylightSavingsTime": true
                },
                "geoLocation": {
                    "geogLocation": {
                        "srs": "EPSG:4326",
                        "latitude": 44.74977166,
                        "longitude": -71.63120018
                    },
                    "localSiteXY": []
                },
                "note": [],
                "siteType": [],
                "siteProperty": [
                    {
                        "value": "ST",
                        "name": "siteTypeCd"
                    },
                    {
                        "value": "01080101",
                        "name": "hucCd"
                    },
                    {
                        "value": "33",
                        "name": "stateCd"
                    },
                    {
                        "value": "33007",
                        "name": "countyCd"
                    }
                ]
            },
            "variable": {
                "variableCode": [
                    {
                        "value": "00060",
                        "network": "NWIS",
                        "vocabulary": "NWIS:UnitValues",
                        "variableID": 45807197,
                        "default": true
                    }
                ],
                "variableName": "Streamflow, ft³/s",
                "variableDescription": "Discharge, cubic feet per second",
                "valueType": "Derived Value",
                "unit": {
                    "unitCode": "ft3/s"
                },
                "options": {
                    "option": [
                        {
                            "name": "Statistic",
                            "optionCode": "00000"
                        }
                    ]
                },
                "note": [],
                "noDataValue": -999999,
                "variableProperty": [],
                "oid": "45807197"
            },
            "values": [
                {
                    "value": [
                        {
                            "value": "2550",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:00:00.000-05:00"
                        },
                        {
                            "value": "2550",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:15:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:30:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:45:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:00:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:15:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:30:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:45:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:00:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:15:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:30:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:45:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:00:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:15:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:30:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:45:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:00:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:15:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:30:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:45:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:00:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:15:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:30:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:45:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:00:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:15:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:30:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:45:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:00:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:15:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:30:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:45:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:00:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:15:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:30:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:45:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T03:00:00.000-05:00"
                        }
                    ],
                    "qualifier": [
                        {
                            "qualifierCode": "P",
                            "qualifierDescription": "Provisional data subject to revision.",
                            "qualifierID": 0,
                            "network": "NWIS",
                            "vocabulary": "uv_rmk_cd"
                        }
                    ],
                    "qualityControlLevel": [],
                    "method": [
                        {
                            "methodDescription": "",
                            "methodID": 66577
                        }
                    ],
                    "source": [],
                    "offset": [],
                    "sample": [],
                    "censorCode": []
                }
            ],
            "name": "USGS:01129500:00060:00000"
        }
    ]
},
"nil": false,
"globalScope": true,
"typeSubstituted": false

}

How do I grab all of the information with the label "value" and store these numbers into a data structure I can then perform functions on?

Thanks


Solution

  • That question has nothing to do with busy parsing and the json itself isn't that large. You could just iterate over $data['value']['timeSeries'][0]['values'][0]['value'] and extract everything you need.

    $data = json_decode($json, true);
    $t = [];
    foreach ($data['value']['timeSeries'][0]['values'][0]['value'] as $item) {
        $t[] = $item['value'];
    }
    

    If you need different keys, just have a look at the json data and modify the loop. If you need unique keys, use array_unique on the $t var.