2

Currently been working on eliminating the excess "," comma on the json object I have below.

{"rules": {
    "1000": {
        "action": "2",
        "category": "skype",
        "entity": "Private",
        "id": "1000",
    },
    "1200": {
        "action": "2",
        "category": "http",
        "entity": "Public",
        "id": "1200",
    },
    "100": {
        "action": "2",
        "category": "ftp",
        "entity": "Public",
        "id": "100",
    },
    "0": {
        "entity": "Private",
        "category": "alcohol, tobacco",
        "action": "1",
        "id": "low",
    },
    "3000": {
    } }}

Maybe you have some insights on what's the cleanest way to eliminate it using AngularJS.

The data was parsed from this code snippet.

var request = {
                url: 'sample/uri',
                method: "GET",
                transformResponse: specialTransform
            };


            var response = $q.defer( );
                $http( request ).success( function( THIS DATA -> data, status ) {
bluestella
  • 383
  • 2
  • 6
  • 17

2 Answers2

3
  1. eval

    var fixTrailingCommas = function (jsonString) {
        var jsonObj;
        eval('jsonObj = ' + jsonString);
        return JSON.stringify(jsonObj);
    };
    
    fixTrailingCommas('{"rules": { "1000": { "action": "2", "category": "skype", "entity": "Private", "id": "1000" ,   } } }');
    

    Please use eval here only if you completely trust incoming json, and also be aware of other eval evils as described on MDN and its note on JSON parsing

    Note that since JSON syntax is limited compared to JavaScript syntax, many valid JavaScript literals will not parse as JSON. For example, trailing commas are not allowed in JSON, and property names (keys) in object literals must be enclosed in quotes. Be sure to use a JSON serializer to generate strings that will be later parsed as JSON.

  2. You may also choose to rely on implementation of JSON2 by Douglas Crockford which uses eval internally

    On current browsers, this file does nothing, preferring the built-in JSON object. There is no reason to use this file unless fate compels you to support IE8, which is something that no one should ever have to do again.

    But because we really need to use this library, we have to make few code modifications, e.g. simply comment out JSON type check, which will then override native browser object (or we may also introduce new JSON2 global variable)

    //if (typeof JSON !== 'object') {
        JSON = {};
    //}
    

    P.S. Other parsing fuctions json_parse.js and json_parse_state.js, which don't use eval, throw a syntax error

  3. Angular part

    var config = {
        transformResponse: function (data, headers) {
            if(headers("content-type") === "application/json" && angular.isString(data)) {
    
                try {
                    data = JSON.parse(data);
                } catch (e) {
                    // if parsing error, try another parser
                    // or just fix commas, if you know for sure that the problem is in commas
                    data = JSON2.parse(data);
                }
    
                return data;
    
            } else {
                return data;
            }
        }
    };
    
    $http.get("rules.json", config).success(function (data) {
        $scope.rules = data;
    });
    
sbedulin
  • 4,102
  • 24
  • 34
  • A cleaver solution but with security and performance problems: http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – frhack Jun 19 '15 at 13:12
  • 1
    Agree, it should be used wisely, if source apis return trusted json. `JSON.js` previously used eval to parse json (need to find a proof, but I clearly remember digging into its source codes somewhere in 2010) – sbedulin Jun 19 '15 at 13:20
0

So as you said, the JSON is wrongly generated on the server you are taking it from, can you change the way it is generated there? (Follow this: Can you use a trailing comma in a JSON object?)

In case you are unable to do so, you need to use something like mentioned here:

Community
  • 1
  • 1
Blaise
  • 7,230
  • 6
  • 43
  • 53