最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using a JSON.parse reviver to obfuscate fields - Stack Overflow

programmeradmin2浏览0评论

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.parse(j, function(key, value) {
  if (key == "name") {        
    return value;
  } else {
    return null;    
  }    
});

The entire json_data object ends up null. In fact, no matter what I make the else, that defines the value of the json_object.

Interestingly, this works as expected:

var json_data = JSON.parse(j, function(key, value) {
  if (key == "name") {        
    return "name";
  } else {
    return value;    
  }    
});

The property "name" now has a value of "name".

JSON in question:

var j = '{"uuid":"62cfb2ec-9e43-11e1-abf2-70cd60fffe0e","count":1,"name":"Marvin","date":"2012-05-13T14:06:45+10:00"}';

Update

I just realized that the inverse of what I want to do works as well so I can nullify the name field:

var json_data = JSON.parse(j, function(key, value) {
  if (key == "name") {        
    return null;
  } else {
    return value;    
  }    
});

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.parse(j, function(key, value) {
  if (key == "name") {        
    return value;
  } else {
    return null;    
  }    
});

The entire json_data object ends up null. In fact, no matter what I make the else, that defines the value of the json_object.

Interestingly, this works as expected:

var json_data = JSON.parse(j, function(key, value) {
  if (key == "name") {        
    return "name";
  } else {
    return value;    
  }    
});

The property "name" now has a value of "name".

JSON in question:

var j = '{"uuid":"62cfb2ec-9e43-11e1-abf2-70cd60fffe0e","count":1,"name":"Marvin","date":"2012-05-13T14:06:45+10:00"}';

Update

I just realized that the inverse of what I want to do works as well so I can nullify the name field:

var json_data = JSON.parse(j, function(key, value) {
  if (key == "name") {        
    return null;
  } else {
    return value;    
  }    
});
Share Improve this question edited Jul 6, 2017 at 1:29 Walery Strauch 7,0728 gold badges54 silver badges60 bronze badges asked May 15, 2012 at 6:39 Toby HedeToby Hede 37.2k28 gold badges137 silver badges164 bronze badges 1
  • Also, what do you see if you console.log -- or alert -- the value in the reviver function? I checked the API and so far your code looks good... – jamesmortensen Commented May 15, 2012 at 6:50
Add a ment  | 

2 Answers 2

Reset to default 4

Through some experimentation, it looks like a final call is made to the function where the key is an empty string and the value is the top-level object:

> JSON.parse('{"hello": "world"}', function(k, v) { console.log(arguments); return v; })
["hello", "world"]
["", Object]

So you could use:

var json_data = JSON.parse(j, function(key, value) {
  if (key == "name" || key === "") {        
    return value;
  } else {
    return null;    
  }    
});

Now, since "" does appear to be a valid JSON key, to be 100% correct it might be better to use something like:

var json_data;
JSON.parse(j, function(key, value) {
  if (key == "name") {        
    return value;
  } else if (key === "") {
    json_data = value;
    return null;
  } else {
    return null;    
  }    
});

But that might be a little bit paranoid ;)

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/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/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.

发布评论

评论列表(0)

  1. 暂无评论