Using a JSON.parse reviver to obfuscate fields

后端 未结 2 1070
梦毁少年i
梦毁少年i 2021-02-06 15:06

I am attempting to abuse a reviver function with JSON.parse.

I basically want to make certain fields \"null\".

If I do this:

var json_data = JSON         


        
2条回答
  •  难免孤独
    2021-02-06 15:39

    It has a rather interesting behavior that the entire object is included in the objects passed to the reviver.

    When the entire object is passed, the key is null.

    http://jsfiddle.net/sGYGM/7/

    var j = '{"uuid":"62cfb2ec-9e43-11e1-abf2-70cd60fffe0e","count":1,"name":"Marvin","date":"2012-05-13T14:06:45+10:00"}';
    
    var json_data = JSON.parse(j, function(k, v) {
        if (k === "" || k == "name") {
            return v;
        } else {
            return null;
        }
    });
    
    console.log(json_data);
    

    As per https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/parse

    The reviver is ultimately called with the empty string and the topmost value to permit transformation of the topmost value. Be certain to handle this case properly, usually by returning the provided value, or JSON.parse will return undefined.

提交回复
热议问题