Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6213725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:44:59+00:00 2026-05-24T06:44:59+00:00

For a Javascript project I have an json string converted into a Javascript object.

  • 0

For a Javascript project I have an json string converted into a Javascript object. But the type of all my values is ‘string’ becaus of the JSON parsing.
Is there any solution to identify the types and let a script convert them into the correct javascript type?

for example

//Javascript object for the json decoded string    
var jsonObj = { id: "foo", count: "1" };

All the values are of type ‘string’ but I want count to be seen as a number. Is there a parser to set the correct type or does it need to be done manual in JS?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:45:00+00:00Added an answer on May 24, 2026 at 6:45 am

    You can use a reviver with JSON.parse.

    json2.js describes the reviver thus

    JSON.parse(text, reviver)
    

    The optional reviver parameter is a function that can filter and
    transform the results. It receives each of the keys and values,
    and its return value is used instead of the original value.
    If it returns what it received, then the structure is not modified.
    If it returns undefined then the member is deleted.

    So to convert count to a number you might do

    JSON.parse(myJsonString, function (key, value) {
      return key === "count" ? +value : value;
    });
    

    so

    JSON.stringify(JSON.parse('{ "id": "foo", "count": "3" }', function (key, value) {
      return key === "count" ? +value : value;
    }));
    

    produces

    {"id":"foo","count":3}
    

    EDIT

    To handle dates as well, you can

    JSON.parse(myJsonString, function (key, value) {
      // Don't muck with null, objects or arrays.
      if ("object" === typeof value) { return value; }
      if (key === "count") { return +value; }
      // Unpack keys like "expirationDate" whose value is represented as millis since epoch.
      if (/date$/i.test(key)) { return new Date(+value); }
      // Any other rules can go here.
      return value;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Maven project with JavaScript code. There is a special javascript compiler
I have one project with is developed in Php and javascript. How to open
I'm working on a security project in Javascript (something I honestly have not used),
We are using jQuery in our project. We have numerous custom javascript files in
When creating an empty MVC 2 project, I have a lot of javascript files
I've been using Javascript's Date for a project, but noticed today that my code
There is a class library project with embedded javascript file, with a lot of
I'm working on a C#/ASP.NET project that has all the javascript files in a
I have set up jqGrid with JSON in my .Net MVC project. The grid
I get an error of Cannot convert object of type System.String' to type 'System.Collections.Generic.List'1[...+ContactHolder]'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.