title: How to change how my JSON looks
body: <p>I have an AJAX call that makes my JSON look this in my JSON file:</p>
  
  <pre><code>{
    "main_object": {
      "id": "new",
      "formData": "language=nl_NL&amp;getExerciseTitle=test&amp;question_takeAudio_exerciseWord%5B0%5D=test&amp;Syllablescounter%5B0%5D=test&amp;Syllablescounter%5B1%5D=test"
    }
  }
  </code></pre>
  
  <p>But I would like to have it like this (and this was before I changed my AJAX call with: <code>id: getUrlParameter('id')</code>:</p>
  
  <pre><code>{
    "main_object": {
      "language": "nl_NL",
      "getExerciseTitle": "asd",
      "question_takeAudio_exerciseWord": ["asd"],
      "Syllablescounter": ["ASDasd", ""]
    }
  }
  </code></pre>
  
  <p>The only thing I want to add is the <code>"id": "new"</code>, but when I do so (with <code>id: getUrlParameter('id')</code> it gets the id, but it messes up my code.</p>
  
  <p>This is my AJAX call (my JSON starts looking like the first piece of code when I use this)</p>
  
  <pre><code>function saveExerciseAjaxCall() {
    $("#my_form").on("submit", function(event) {
      event.preventDefault();
      $.ajax({
        url: 'saveJson.php',
        type: 'POST',
        data: {
          id: getUrlParameter('id'),
          formData: $('#my_form').serialize()
        },
        dataType: 'json',
      }).done(function(response) {
  
      });
    });
  }
  </code></pre>
  
  <p>How could this be achieved? I know it isn't my <code>saveJson.php</code> that's the problem. I know it's my AJAX call since it starts showing like the first JSON piece of code when I add the <code>id: getUrlParameter('id')</code>.</p>
  
  <p>edit: Since someone said it could be my php code.. here is my php code:</p>
  
  <pre><code>&lt;?php
  include_once('database_json.php');
  $data = $_POST;
  //Setup an empty array.
  $errors = array();
  if (isset($data)) {
      $newExerciseData['main_object'] = $data;
      $exerciseArray = $data['main_object'];
      $databaseFile = 'json_files/database.json';
      $textContent = file_get_contents($databaseFile);
      $database = json_decode($textContent, true);
      if ($data['id'] === 'new') {
          if (count($database['data']) == 0) {
              $ID = 0;
          } // ending database['data'] count 0.
          else {
              $maxID = max($database['data']);
              $ID = ++$maxID["id"];
           } // ending the max ID else statement.
          $newJsonFile = 'jsonData_' . $ID . '.json';
          $newJsonFilePath = 'json_files/' . $newJsonFile;
          //Create new database exercise_txt
          $newArrayData = array(
          'id' =&gt; $ID,
          'exercisetitle' =&gt; $data['formData']['getExerciseTitle'],
          'language' =&gt; $data['formData']['language'],
          'file' =&gt; $newJsonFile
      );
      $database['data'][] = $newArrayData;
      file_put_contents($databaseFile, json_encode($database, JSON_UNESCAPED_UNICODE, JSON_PRETTY_PRINT));
      file_put_contents($newJsonFilePath, json_encode($newExerciseData, JSON_UNESCAPED_UNICODE, JSON_PRETTY_PRINT));
  } // } op regel 34 lijkt verdwaald...?
  else {
      $index = array_search((int) $_POST['id'], array_column($database['data'], 'id'));
      $correctJsonFile = 'json_files/jsonData_' . $_POST['id'] . '.json';
      $newJsonFile = 'jsonData_' . $_POST['id'] . '.json';
      $newJsonFilePath = 'json_files/' . $newJsonFile;
      //Create new database exercise_txt
      $newArrayData2 = array(
          'id' =&gt; (int) $data['id'],
          'exercisetitle' =&gt; $data['formData']['getExerciseTitle'],
          'language' =&gt; $data['formData']['language'],
          'file' =&gt; $newJsonFile
      );
      $database['data'][$index] = $newArrayData2;
      file_put_contents($databaseFile, json_encode($database, JSON_UNESCAPED_UNICODE));
      file_put_contents($newJsonFilePath, json_encode($newExerciseData, JSON_UNESCAPED_UNICODE));
  } // closing off the else statement
   echo json_encode($newExerciseData, JSON_UNESCAPED_UNICODE);
  } //closing off the if isset.
  ?&gt;
  </code></pre>