Skip to content
Advertisement

How to parse Minecraft ops.json to display all oped users, their UUID’s and the total amount of oped users

Sorry if the title of this doesn’t make sense, I’m very new to working with JSON files in PHP. I looked almost everywhere and reworded my question multiple times, every answer I get doesn’t seem to work my JSON file, not sure if Minecraft does something different or if I’m missing something. Thank you to anyone who responds to this!

Goal

List all operators on my Minecraft Server on my website in a proper format with their UUID’s and OP Level. I would also like to show the current amount of people OPed on the server.

Example Photo of what I imagine it looking like (of course with better CSS)

Here’s my PHP Code

$serverdir = "../Minecraft Server/";
$json_url = $serverdir."ops.json";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);

<?php
foreach ($data as $value) {
  echo "$value <br>";
}

This code currently just spits out Array 5 times I have to change $data to $data[1-5] to get an actual output but it outputs the username, UUID, and level but I don’t see how to format this type of output.

Here’s the JSON File

[
  {
    "uuid": "3afa9281-a239-453f-9538-49b57d9f2d06",
    "name": "Deathx90",
    "level": 2,
    "bypassesPlayerLimit": false
  },
  {
    "uuid": "461e6427-1d5c-43e3-b4ee-f8361884c3a2",
    "name": "Command_String",
    "level": 2,
    "bypassesPlayerLimit": false
  },
  {
    "uuid": "1ab6c2d3-6cae-4937-81af-cf348ca0f16c",
    "name": "sarcazsm",
    "level": 2,
    "bypassesPlayerLimit": false
  },
  {
    "uuid": "83619f6c-d259-4da2-868b-eba807e15b37",
    "name": "Monke_Syndrome",
    "level": 2,
    "bypassesPlayerLimit": false
  },
  {
    "uuid": "584f7a2f-2f8c-453e-a459-6ce8a683f04c",
    "name": "Kreyo813",
    "level": 2,
    "bypassesPlayerLimit": false
  }
]

After Solving:

Here is the final product, I add some additional looks to it as well.

Advertisement

Answer

You can also do it with php object notation

$Object = json_decode($json);
foreach($Object as $obj){
    echo $obj->uuid,'<br>',
         $obj->name,'<br>',
         $obj->level,'<br>';
}

You can use count to get number of users, example only

$Object = json_decode($json);
$count = count($Object);

echo '<div><p>Number of OPed Users: (',$count,')</p></div>';
foreach($Object as $obj){
   echo '<div><p>',$obj->uuid,'<br>',
         $obj->name,'<br>',
         $obj->level,'</p></div><hr>';
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement