Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Process json file, remove key / value pairs and Capitalize names JavaScript


FILE = "csvjson.json" ~3000 records

{"ID":16720664,"Name":"ADEYEMON, MURIE","ST":"NY","Exp":"2019-05-31","Rate":0},
{"ID":12901834,"Name":"AGOK, PETER PANTHER","ST":"NY","Exp":"2019-03-31","Rate":1771},
{"ID":16350271,"Name":"BAZANT, TOMASZ","ST":"NY","Exp":"2018-04-30","Rate":1965}

Using javascript (or jquery), I want to delete the "ID" and the "ST" key and value pairs in this JSON object.

<script src="js/jquery-3.3.1.min.js"></script>

<script>var members = <?php include('csvjson.json'); ?></script>

Guessing this is simple enough:

delete members.ID;
delete members.ST;

Then I want to Capitalize (from UPPERCASE) all "Name" values:

"Name":"ADEYEMON, MURIE" ====> "Name":"Adeymon, Murie"
"Name":"AGOK, PETER PANTHER" ====> "Name":"Agok, Peter Panther"
"Name":"BAZANT, TOMASZ" ====> "Name":"Bazant, Tomasz"

(Write this to a file. Maybe "temp.json")

Then I need to fix some Irish names (which I did in php, but a javascript / jquery solution is preferable)
Please use php only if necessary!

$X0 = file_get_contents("temp.json");

$X1 = str_replace ("O'heron","O'Heron",$X0);
$X2 = str_replace ("Mckay","McKay",$X1);
$X3 = str_replace ("Mcbride","McBride",$X2);
$X4 = str_replace ("Mackenzie,","MacKenzie,",$X3);

file_put_contents("uscf.json", $X4);

Then beautify the minified "temp.json" by replacing "}," with "},\n"

Answers (2)


Rainner Lins answers:

Here's a way to do it using just JS. You only need to loop ove the data once and do the changes to each item in the list, something like this:

// old data
const oldData = [
{"ID":16720664,"Name":"ADEYEMON, MURIE","ST":"NY","Exp":"2019-05-31","Rate":0},
{"ID":12901834,"Name":"AGOK, PETER PANTHER","ST":"NY","Exp":"2019-03-31","Rate":1771},
{"ID":16350271,"Name":"BAZANT, TOMASZ","ST":"NY","Exp":"2018-04-30","Rate":1965},
{"ID":16350271,"Name":"MCKAY, RODNEY","ST":"NY","Exp":"2018-04-30","Rate":1965},
{"ID":16350271,"Name":"MCBRIDE, JOHN","ST":"NY","Exp":"2018-04-30","Rate":1965}

// loop over all the items in old data...
const newData = item => {

// 1. delete unwanted keys
delete item.ID;
delete item.ST;

// 2. capitalize name
item.Name = item.Name.split( /\s+/g ).map( str => {
return str.charAt( 0 ).toUpperCase() + str.slice( 1 ).toLowerCase();
}).join( ' ' );

// 3. do name fix
item.Name = item.Name.replace( 'Mckay', 'McKay' );
item.Name = item.Name.replace( 'Mcbride', 'McBride' );

// return new item
return item;

// beautifyed data
const prettyData = JSON.stringify( newData, null, 2 );

// final result
console.log( prettyData );

That will give you something like this:

"Name": "Adeyemon, Murie",
"Exp": "2019-05-31",
"Rate": 0
"Name": "Agok, Peter Panther",
"Exp": "2019-03-31",
"Rate": 1771

User180435 comments:

I wanted one tuple on each line so I can edit the file directly. Also, how do I write to the destination file, "uscf.json" ?


Arnav Joy answers:

if you are using php, can I do all these changes using php?

User180435 comments:

Yes, that would fine!