2

I couldn't find an answer, so I decided to ask.

I get this response from an API:

[
  {
      "seasonNumber":1,
      "numWins":1,
      "numHighBracket":2,
      "numLowBracket":2,
      "seasonXp":111,
      "seasonLevel":5,
      "bookXp":0,
      "bookLevel":1,
      "purchasedVIP":false
   },
   {
      "seasonNumber":2,
      "numWins":1,
      "numHighBracket":21,
      "numLowBracket":31,
      "seasonXp":1651,
      "seasonLevel":25,
      "bookXp":9,
      "bookLevel":11,
      "purchasedVIP":false
   },
   {
      "seasonNumber":3,
      "numWins":9,
      "numHighBracket":57,
      "numLowBracket":127,
      "seasonXp":4659,
      "seasonLevel":68,
      "bookXp":0,
      "bookLevel":100,
      "purchasedVIP":true
   },
   {
      "seasonNumber":4,
      "numWins":8,
      "numHighBracket":19,
      "numLowBracket":36,
      "seasonXp":274,
      "seasonLevel":33,
      "bookXp":7,
      "bookLevel":35,
      "purchasedVIP":true
   }
]

I am trying to change the json data to this:

{
  "seasons":
    [
      {
        "season":1,
        "battle_pass":false
      },
      {
        "season":2,
        "battle_pass":false
      },
      {
        "season":3,
        "battle_pass":true
      },
      {
        "season":4,
        "battle_pass":true
      }
    ]
}

In my current code I am using regex like this:

preg_match_all("/(?:\{\"seasonNumber\"\:(\w)|purchasedVIP\"\:(\w+))/", $response, $seasons);

echo '{"seasons":'.json_encode($seasons, JSON_FORCE_OBJECT, JSON_PRETTY_PRINT).'}';

It's basically putting everything in a separate array but that's not what I want.

2
  • Decode json to array and filter this array. Commented Sep 22, 2018 at 11:14
  • 2
    Don't hack a json string with regex. Commented Sep 22, 2018 at 11:15

1 Answer 1

3

Decode the json, restructure the data, re-encode.

Code: (Demo)

// your $json = 
foreach (json_decode($json) as $set) {
    $array[] = ["season" => $set->seasonNumber, "battle_pass" => $set->purchasedVIP];
}
echo json_encode(["seasons" => $array]);

Output:

{"seasons":[{"season":1,"battle_pass":false},{"season":2,"battle_pass":false},{"season":3,"battle_pass":true},{"season":4,"battle_pass":true}]}

p.s. if you want to force objects and pretty print, separate those flags with a pipe (|). https://3v4l.org/qsPb0

Sign up to request clarification or add additional context in comments.

5 Comments

That is an insanely long portion of json and my phone is a poor tool to handle it. After decoding your json, save the array as a variable. You might like to use $array = json_decode($json, true); so that you can use array syntax (instead of object arrows). Then print out what is in the array, with var_export($array);. Now you will see the first level keys that are available -- like profileChanges. If you want to drill down that subarray, edit your var_export() call to be var_export($array["profileChanges"]); and run your file again. Keep drilling until you reach the desired subarray.
Keep adding the targeted keys after $array using square bracket syntax like var_export($array["key1"]["key2"]["etc"]).
oof man thanks for helping I'll try. if I'll have any issues I'll ask you
Ok so I have this code $response2_decocded = json_decode($response2, true); foreach ($response2['profileChanges']['stats']['attributes']['past_seasons'] as $set) { $array_seasons[] = ["season" => $set['seasonNumber'], "battle_pass" => $set['purchasedVIP']]; } echo json_encode(["seasons" => $array_seasons]); And the response is {"seasons":null}
Your input string was too big for my preferred demo site, so I left out the json declaration. I did test my code on my localhost host to be effective though. 3v4l.org/p4cMO I have included some array_key() calls to show you another simple way to investigate the array tree structure. You were pretty close. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.