Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to use function in JSON.parse with JavaScript

3 Answers

0 votes
const json = '{"1": "A", "2": "B", "3": "C", "4": "D", "5": "E"}';

const parsedJson = JSON.parse(json, (key, value) => {
    if (key == 3)
        console.log('key == 3');

    if (key == 5)
		console.log('key == 5');

    return value; 
});

console.log(parsedJson);



  
/*
run:

key == 3
key == 5
{ '1': 'A', '2': 'B', '3': 'C', '4': 'D', '5': 'E' }

*/

 



answered Mar 20, 2020 by avibootz
0 votes
const json = '{"1": "A", "2": "B", "3": "C", "4": "D", "5": "E"}';
 
const parsedJson = JSON.parse(json, (key, value) => {
	console.log(key, value);
});
 
 
 
   
/*
run:
 
1 A
2 B
3 C
4 D
5 E
 {}
 
*/

 



answered Mar 20, 2020 by avibootz
0 votes
const json = '{"1": "A", "2": "B", "3": "C", "4": "D", "5": "E"}';
 
const parsedJson = JSON.parse(json, (key, value) => {
    if (key == 3)
        return 'JavaScript';
 
    if (key == 5)
		return 'Node.js';
 
    return value; 
});
 
console.log(parsedJson);
 
 
 
   
/*
run:
 
{ '1': 'A', '2': 'B', '3': 'JavaScript', '4': 'D', '5': 'Node.js' }
 
*/

 



answered Mar 20, 2020 by avibootz
edited Mar 20, 2020 by avibootz

Related questions

...