2

I'm trying to get the following formatting in JSON using PHP which is querying the database... notice there is no final comma

[
      {
        "ID": "4",
        "Name": "Jill Higgins",
        "Job Title": "Designer",
        "Organisation": "Design Widget",
        "Organisation Type": "Academia",
        "Latitude": "54.669978",
        "Longitude": "-1.452469"
    },
    {
        "ID": "5",
        "Name": "Bob Billy",
        "Job Title": "Clown",
        "Organisation": "Big Comp INC",
        "Organisation Type": "Company",
        "Latitude": "54.669978",
        "Longitude": "-1.452469"
    }
]

This is my code currently...

if (PerchUtil::count($members)) {

echo '[';

foreach ($members as $Member) {
  //prepare the data
  $data = array(
    'ID' => $Member->memberID(),
    'Name' => $Member->first_name() . ' ' . $Member->last_name(),
    'Job Title' => $Member->expert_job_title(),
    'Organisation' => $Member->expert_org_name(),
    'Organisation Type' => $Member->expert_org_type(),
    'Latitude' => $Member->expert_org_latitude(),
    'Longitude' => $Member->expert_org_longitude()
  );
}
echo ']';
}

header('Content-Type: application/json');

This is what it currently looks like... notice there is a comma at the end which I don't need. The spacing isn't really helpful with the brackets either... how do I amend the PHP so it is cleaner and counts out the final comma?

[{
    "ID": "4",
    "Name": "Jill Higgins",
    "Job Title": "Designer",
    "Organisation": "CPI",
    "Organisation Type": "Academia",
    "Latitude": "54.669978",
    "Longitude": "-1.452469"
},{
    "ID": "5",
    "Name": "Bob Billy",
    "Job Title": "Clown",
    "Organisation": "Big Comp INC",
    "Organisation Type": "Company",
    "Latitude": "54.669978",
    "Longitude": "-1.452469"
},]
Dan Lee
  • 684
  • 4
  • 15
  • 35
  • 4
    Build the data into an array (something like `$data[] =` ) and then `json_encode()` the result – Nigel Ren Jun 26 '18 at 14:22
  • How do I do this? – Dan Lee Jun 26 '18 at 14:24
  • 1
    You should not manually manipulate json strings like this, what are you trying to make it pretty for? Some display purpose? – ajmedway Jun 26 '18 at 14:24
  • Readability. To clarify are you referencing the echo statments? – Dan Lee Jun 26 '18 at 14:25
  • You can pass the JSON_PRETTY_PRINT flag, read the manual for json_encode – Devon Bessemer Jun 26 '18 at 14:26
  • I'm already using it – Dan Lee Jun 26 '18 at 14:27
  • Apparently closed this before I could answer, use `JsonSerialize` , probably better tackling the root cause: https://pastebin.com/UqtJm111 – Mikey Jun 26 '18 at 14:30
  • @Mikey, my doubt is that with `JsonSerialize` is that your wrapping the how to encode something in JSON into your model class (Member in this instance). Especially as Name is made up of two fields, so this may not be generally how you would want your JSON to be encoded. After all you wouldn't include a 'display the member details in HTML' in the same class. – Nigel Ren Jun 27 '18 at 14:30
  • Thanks for your help! – Dan Lee Jun 27 '18 at 15:25

1 Answers1

6

Create an array of the data you want to encode in $data, add each item using $data[] = and then echo json_encode($data); to get the right format...

if (PerchUtil::count($members)) {
    $data = [];
    foreach ($members as $Member) {
      //prepare the data
      $data[] = array(
         'ID' => $Member->memberID(),
         'Name' => $Member->first_name() . ' ' . $Member->last_name(),
         'Job Title' => $Member->expert_job_title(),
         'Organisation' => $Member->expert_org_name(),
         'Organisation Type' => $Member->expert_org_type(),
         'Latitude' => $Member->expert_org_latitude(),
         'Longitude' => $Member->expert_org_longitude()
      );
    }
    echo json_encode($data, JSON_PRETTY_PRINT);
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55