Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript JSON parse object property directly to int

Tags:

javascript

I have some objects that I'm parsing from json using native browser implementations. Some of the objects' properties are numbers. For the moment, the numbers are parse from json as strings and I use parseInt to cast the string to the int I need.

The problem is that I've got 23 objects I do this with and overall about 80 properties that I'm parsing to ints like this:

if (TheObject && TheObject.TheProperty) {
   TheObject.TheProperty = parseInt(TheObject.TheProperty, 10);
}

There are many lines of code that look very similar. Is there a way using prototypes or something to change the way the JSON.parse function works so that each time the parser runs it checks to see if a string property is actually an int and if so cast it directly as such?

Thanks.

like image 496
frenchie Avatar asked May 12 '12 12:05

frenchie


People also ask

Can JavaScript parse JSON natively?

JSON is a JavaScript-based object/value encoding format that looks very close to raw JavaScript and can be very easily parsed by JavaScript code because JavaScript can effectively evaluate a JSON string and re-materialize an object from it.

How do you parse an object in JavaScript?

Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

What does JSON Stringify () method do?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Can JSON take integers?

The integer type is used for integral numbers. JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1 and 1.0 are two ways to represent the same value in JSON.


2 Answers

JSON.parse accepts a second argument in the form of a function that can do some post processing.

JSON.parse('{"p": "5"}', function(k, v) { 
    return (typeof v === "object" || isNaN(v)) ? v : parseInt(v, 10); 
});

I you don't want to process all numeric strings, then create a lookup table of the properties you do want.

var props = {"p":1, "some_prop":1, "another_prop":1};

JSON.parse('{"p": "5"}', function(k, v) { 
    return props.hasOwnProperty(k) ? parseInt(v, 10) : v; 
});
like image 174
cliffs of insanity Avatar answered Nov 15 '22 21:11

cliffs of insanity


JSON can handle numbers as follow:

{
    "TheObject":{
        "TheProperty":5
    }
}

If your property was doublequoted then it's a string else it's a numeric, boolean (true and false values), null or just something that cause parse error.

See http://json.org/

like image 36
kbec Avatar answered Nov 15 '22 21:11

kbec