Search code examples
jsongravity-forms-plugingravityforms

Gravity Forms Web API - Field Names with fullstops issue


I'm using the Web API to download form entries into an offline system and having an issue with my JSON parser with some of the form field IDs. For example I receive an entry with fields like this:

{
  "response": {
    "entries": [
      {  
      "3.3": "Henry",
      "3.6": "Ford",
      "3.2": "",
      "3.4": "",
      "status": "active", 
      "transaction_id": null, 
      "transaction_type": null, 

The period/fullstop in the field ID is throwing out my JSON parser which uses the period/fullstop as the separator ($.response.entries[0].3). Is there a way to change the period/fullstops to underscores of have the API return the name of the field instead like it does for "transaction_type" etc?


Solution

  • A hacky solution if you can't change how the data is coming from the API would be to pre-parse it yourself. You can do something like this using a simple string replace:

    var data = {
      "response": {
        "entries": [
          {  
          "3.3": "Henry",
          "3.6": "Ford",
          "3.2": "",
          "3.4": "",
          "status": "active", 
          "transaction_id": null, 
          "transaction_type": null, 
          }
         ]
        }
    }
    
    data = JSON.stringify(data)
    data = data.replace(/\./g, '_');
    data = JSON.parse(data)
    
    console.log(data)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>