0

I have List of IDs in JSON

Here it is: https://temp.9animetv.live/api.php

it look like this

 { "result": [
    548915,
    505031,
    28967,
    520928,
    441762,
    381418,
    61650,
    249457,
    535995,
    550023,
      and more.. and more.. and more..
   ]
}

I want to change it to be like this

{ 
    "result": [
        {"id": 548915,}, 
        {"id": 505031,}, 
        {"id": 28967,},
        {"id": 28967,}, 
        {"id": 28967,}
      ] 
}

How to do that? Am using PHP

I have Tried

To change it to Array using Json_decode But still haven't figured out a way to do as I expected

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

1

Simply read the json string, convert it to a PHP equivalent object using json_decode() process the array of id's and create a new array which you then convert back to a JSON String

$j_str = file_get_contents('https://temp.9animetv.live/api.php');
$j_arr = json_decode($j_str);
$new = [];
foreach ( $j_arr->result as $occ ) {
    $new[] = ['id' => $occ];
}
echo json_encode($new, JSON_PRETTY_PRINT);

RESULTS

[
    {
        "id": 548915
    },
    {
        "id": 505031
    },
    {
        "id": 28967
    },

    . . .

You can skip the , JSON_PRETTY_PRINT, thats only I could easily read the output for checking

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149