Questions tagged [reviver-function]

A reviver function transforms the results returned from the `parse` method of the JavaScript JSON API.

The reviver function is declared as an optional argument passed to the parse method. After JSON parsing is complete, it is called for each member of the object. The return value of each call replaces the original value. If it returns undefined, then the property is deleted from the result. If a member contains nested objects, then the nested objects are transformed before the parent object.

16 questions
12
votes
2 answers

how to use reviver function with fetch.response.json()

There is a "reviver" function that comes with JSON.parse (eg: "JSON.parse using reviver function"). How can I use this "reviver" with response.json? for instance: fetch(url) .then(a => a.json({reviver})) // unfortunately not working .then(...) I…
allez l'OM
  • 547
  • 4
  • 13
9
votes
1 answer

How to create a map of records from a javascript raw object with Immutable.js?

I'm new to immutable.js and I'd like to understand better how to use records starting from a raw JS object. With Immutable.fromJS() I can create a map passing a raw object, for example: var images = { "1": { id: "1", urls:…
gpbl
  • 4,721
  • 5
  • 31
  • 45
9
votes
7 answers

How to use JSON.parse reviver parameter to parse date string

My JSON string contains a date field that returns such a value: "2009-04-04T22:55:16.0000000-04:00" I am particularly interested in parsing only the date compartment not the time. I tried using a reviver function, but interestingly the reviver…
Aleyna
  • 1,857
  • 4
  • 20
  • 27
4
votes
2 answers

Using a JSON.parse reviver to obfuscate fields

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…
Toby Hede
  • 36,755
  • 28
  • 133
  • 162
2
votes
0 answers

Express: Body parser JSON reviver not working as expected

I'm using Node and Express, hosting in Firebase Functions to build an API to persist the data. When I send a JSON object to a router, the parser works ok, but when I have Dates, they are parsed as string, causing some trouble to save the data…
Victor de Almeida
  • 990
  • 2
  • 7
  • 7
2
votes
1 answer

JSON.parse reviver function has n+1 keys?

I wanted to test the code overload which can provide a reviver function when parsing a JSON string. So this code: JSON.parse('{"p": 5}', function(k, v) { if (k === "") return v; return v * 2; }).p; yields 10 (ok). But then I asked myself, 'what is…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
1
vote
2 answers

JSON parse reviver

I'm trying to parse a list of JSON objects into a variable called jsonStructure with a reviver function which adds 5 to the 'year' object within the stringData variable. However, the variable returns undefined. I'm not sure what I'm doing wrong as I…
1
vote
1 answer

JSON.parse reviver function nested objects

My goal is to tell the JSON.parse function with the optional reviver argument that I would like it if the string being passed to the calc(string) function has a key named "expr" to perform the operation inside of that expression, and then continue…
giggidy
  • 75
  • 1
  • 1
  • 8
1
vote
1 answer

node.js JSON.parse reviver doesn't report duplicate keys?

I'm trying to use a JSON parser which will detect and save duplicate keys. I'm using JSON.parse() in node.js with a reviver, which I am hoping will tell me when I'm getting a duplicate key. However it doesn't. Is there another way? Is there a…
John Carlson
  • 321
  • 1
  • 13
1
vote
1 answer

Javascript: Using reviver function, I seem can't get to alter all the keys, while concating the numbers

I just want to change all the keys in batchesX. But I can't seem to alter all keys, because of concat. This is what I learned from post. Please advise how I can change all keys with numbers. var batchesX =…
user1739825
  • 820
  • 4
  • 10
  • 27
0
votes
1 answer

JavaScript JSON reviver in Python

I'm having problem translating my JavaScript snippet to Python. The JavaScript code looks like this: const reviver = (_key, value) => { try { return JSON.parse(value, reviver); } catch { if(typeof value === 'string') { const…
0
votes
1 answer

Better / Faster way to extract value with a matched property on different depth on a nested object with condition

I have an object with inconsistent nesting structure that I have no control over. I need to extract certain value if it met a certain condition. currently doing it by iterating over the properties recursively and matching the properties with the…
pokken
  • 327
  • 1
  • 15
0
votes
1 answer

JSON reviver function returns undefined even with return

function _quotedText(data, config) { var pathKeys=config.customKey; console.log(pathKeys); //prints os_platform var inAuth_data=(JSON.parse(JSON.parse(JSON.parse(data["event.fields.custom_fields.inauth_device_data"])), (key,…
Anusha
  • 204
  • 1
  • 4
  • 12
0
votes
1 answer

Unexpected behavior in JSON.parse Reviver function, deleting the object and not the key properties

JSBin link so that you can run the code quickly. JSbinhere The problem is in the comments, but from what the documentation states about how the reviver (that name is terrible), works, if you do not return a value or if you return undefined then that…
Michael Ryan Soileau
  • 1,763
  • 17
  • 28
0
votes
1 answer

Why would a WCF AJAX method's argument not be populated with values, even though the instance is not null?

I sent an AJAX request to my server using jQuery.ajax, and the "data" field is populated with a JSON string corresponding to my ClientMessage type, but the data isn't making it to the method's main parameter. All members of ClientMessage are null…
Triynko
  • 18,766
  • 21
  • 107
  • 173
1
2