-4

I am new to the coding scene and am attempting to retrieve a particular value from a given JSON file and store it as a PHP variable to then add it to an SQL database.

I have gone through about half a dozen tutorials and they only seem to go 1 layer deep and get a value from their is that makes sense.

I am trying to get to the bolded values in php.

features{"attributes":{"Master_Incident_Number":"QF2-19-097369","IncidentType":"Vegetation Fire" ....

Full JSON file here

Is this possible, if so can someone please help point me in the path to do so.

Thank you

1 Answers1

0

Use map to iterate over features.attributes to access keys Master_Incident_Number and IncidentType.

const input = {
  "features": [{
      "attributes": {
        "Master_Incident_Number": "QF4-20-011913",
        "IncidentType": "Vegetation Fire",
        "ResponseDate": 1580279411000,
        "LastUpdate": 1580357356000,
        "Location": "457 Ten Chain Rd, Kinleymore",
        "Region": "4 North Coast Region",
        "Latitude": -26.208141,
        "Longitude": 151.672965,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 1,
        "VehiclesOnScene": 0,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4726048",
        "OBJECTID": 1,
        "GlobalID": "5c6e0a07-aa7d-4e80-a44e-b652d2f0f18b",
        "Link": "https://"
      },
      "geometry": {
        "x": 151.672965,
        "y": -26.208141
      }
    },
    {
      "attributes": {
        "Master_Incident_Number": "QF4-20-012241",
        "IncidentType": "Permitted Burn (Attendance)",
        "ResponseDate": 1580353993000,
        "LastUpdate": 1580356129000,
        "Location": "22 Almond Rd, Benarkin North",
        "Region": "4 North Coast Region",
        "Latitude": -26.832734,
        "Longitude": 152.156489,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 0,
        "VehiclesOnScene": 4,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4798706",
        "OBJECTID": 2,
        "GlobalID": "2726c898-a6f4-4906-804f-d3f2e6c8a087",
        "Link": "https://"
      },
      "geometry": {
        "x": 152.156489,
        "y": -26.832734
      }
    },
    {
      "attributes": {
        "Master_Incident_Number": "QF6-20-012251",
        "IncidentType": "Vegetation Fire",
        "ResponseDate": 1580354969000,
        "LastUpdate": 1580358095000,
        "Location": "386 Main Western Rd, Tamborine Mountain",
        "Region": "6 South Eastern Region",
        "Latitude": -27.955333,
        "Longitude": 153.182491,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 2,
        "VehiclesOnScene": 1,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4813494",
        "OBJECTID": 3,
        "GlobalID": "e310dc73-695c-4466-8dc4-e6eb5344b406",
        "Link": "https://"
      },
      "geometry": {
        "x": 153.182491,
        "y": -27.955333
      }
    },
    {
      "attributes": {
        "Master_Incident_Number": "QF4-20-012274",
        "IncidentType": "Vegetation Fire",
        "ResponseDate": 1580357366000,
        "LastUpdate": 1580357666000,
        "Location": "2-21 Steindl St, Bundaberg East",
        "Region": "4 North Coast Region",
        "Latitude": -24.865996,
        "Longitude": 152.37337,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 1,
        "VehiclesOnScene": 0,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4828626",
        "OBJECTID": 4,
        "GlobalID": "a37695bd-ef88-49bd-813a-ca3133d42d83",
        "Link": "https://"
      },
      "geometry": {
        "x": 152.37337,
        "y": -24.865996
      }
    }
  ]
};

const output = input.features.map(({
  attributes: {
    Master_Incident_Number,
    IncidentType
  }
}) => console.log(Master_Incident_Number, IncidentType));

-- Edit --

const input = {
  "features": [{
      "attributes": {
        "Master_Incident_Number": "QF4-20-011913",
        "IncidentType": "Vegetation Fire",
        "ResponseDate": 1580279411000,
        "LastUpdate": 1580357356000,
        "Location": "457 Ten Chain Rd, Kinleymore",
        "Region": "4 North Coast Region",
        "Latitude": -26.208141,
        "Longitude": 151.672965,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 1,
        "VehiclesOnScene": 0,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4726048",
        "OBJECTID": 1,
        "GlobalID": "5c6e0a07-aa7d-4e80-a44e-b652d2f0f18b",
        "Link": "https://"
      },
      "geometry": {
        "x": 151.672965,
        "y": -26.208141
      }
    },
    {
      "attributes": {
        "Master_Incident_Number": "QF4-20-012241",
        "IncidentType": "Permitted Burn (Attendance)",
        "ResponseDate": 1580353993000,
        "LastUpdate": 1580356129000,
        "Location": "22 Almond Rd, Benarkin North",
        "Region": "4 North Coast Region",
        "Latitude": -26.832734,
        "Longitude": 152.156489,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 0,
        "VehiclesOnScene": 4,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4798706",
        "OBJECTID": 2,
        "GlobalID": "2726c898-a6f4-4906-804f-d3f2e6c8a087",
        "Link": "https://"
      },
      "geometry": {
        "x": 152.156489,
        "y": -26.832734
      }
    },
    {
      "attributes": {
        "Master_Incident_Number": "QF6-20-012251",
        "IncidentType": "Vegetation Fire",
        "ResponseDate": 1580354969000,
        "LastUpdate": 1580358095000,
        "Location": "386 Main Western Rd, Tamborine Mountain",
        "Region": "6 South Eastern Region",
        "Latitude": -27.955333,
        "Longitude": 153.182491,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 2,
        "VehiclesOnScene": 1,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4813494",
        "OBJECTID": 3,
        "GlobalID": "e310dc73-695c-4466-8dc4-e6eb5344b406",
        "Link": "https://"
      },
      "geometry": {
        "x": 153.182491,
        "y": -27.955333
      }
    },
    {
      "attributes": {
        "Master_Incident_Number": "QF4-20-012274",
        "IncidentType": "Vegetation Fire",
        "ResponseDate": 1580357366000,
        "LastUpdate": 1580357666000,
        "Location": "2-21 Steindl St, Bundaberg East",
        "Region": "4 North Coast Region",
        "Latitude": -24.865996,
        "Longitude": 152.37337,
        "CurrentStatus": "Going",
        "VehiclesOnRoute": 1,
        "VehiclesOnScene": 0,
        "CurrentWarning": "Information",
        "MediaMessage": "There is a vegetation fire at this location. Smoke may affect residents and motorists in the area. Residents should close their doors and windows and keep medication close by if suffering from a respiratory condition. Motorists should use caution and drive to conditions. If you believe your property is under threat, you should call Triple Zero (000) immediately.",
        "FMETimestamp": "20200130142217.4828626",
        "OBJECTID": 4,
        "GlobalID": "a37695bd-ef88-49bd-813a-ca3133d42d83",
        "Link": "https://"
      },
      "geometry": {
        "x": 152.37337,
        "y": -24.865996
      }
    }
  ]
};

const output = input.features.map((obj) => console.log(obj.attributes.Master_Incident_Number, obj.attributes.IncidentType));
random
  • 7,756
  • 3
  • 19
  • 25
  • is there way to call that JSON file from a directory like open("data.json") kind of thing or even from a url? or do you have to lay it out like you did? and can you please explain the **const output = input.features.map(({ ** please and what does **}) => console.log(Master_Incident_Number, IncidentType));** do? So that I may learn and not have to pester and annoy people online for help – Cooper Davies Mar 30 '20 at 10:54
  • @CooperDavies - In javascript, you can use `fetch` method or jquery getJSON method - https://api.jquery.com/jQuery.getJSON/. – random Mar 30 '20 at 10:57
  • is there a way to do that in PHP, I am trying to do everything in PHP is possible so I can add the values up into an SQL database? – Cooper Davies Mar 30 '20 at 11:00
  • @CooperDavies - Sure. Don't hesitate to ask. I have added another example in edit to remove confusion. `map` function is used to iterate over array. In the `.map({})`, I'am doing `object destructuring` - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment. It is basically extracting the object property and storing in another variable. `{ attributes: { Master_Incident_Number, IncidentType }`. In this first I'am extracting the `attributes property` and then `Master_Incident_Number` and `IncidentType` which are properties of `attributes` object. – random Mar 30 '20 at 11:05
  • I don't know about `php`. – random Mar 30 '20 at 11:05
  • So you have the output = the mapped out JSON file which then allows you to log the master incident number through the mapped out obj under attributes, is that correct? Is there a way to 'map' out the JSON file without having it laid out in the code itself, like if you called it from a directory? and, sorry to be a pain, but is your example in Javascript or PHP? – Cooper Davies Mar 30 '20 at 11:10
  • @CooperDavies - It's javascript. Since, this question has `tag` of `javascript`. For `php`, check there official documentation. – random Mar 30 '20 at 11:12
  • Ahh, thank you, I will see how far I can get before burning my house to the ground. Thank you again. next question, and this is going to be a dumb one, so brace yourself, where would someone find said official documentation for the PHP? asking for a friend. and what would happen with the mapping if the JSON file was in a singular line, would that affect anything ? – Cooper Davies Mar 30 '20 at 11:15
  • @CooperDavies - https://www.php.net/manual/en/. – random Mar 30 '20 at 11:18