-2

I have the following json

{
  "Title": "Test",
  "StartDate": {
    "Month": "3",
    "Year": "1973"
  },
  "EndDate": {
    "Month": "4",
    "Year": "1974"
  }
}

I want Month and Year values from StartDate and EndDate to be without quotes, like this:

{
  "Title": "Test",
  "StartDate": {
    "Month": 3,
    "Year": 1973
  },
  "EndDate": {
    "Month": 4,
    "Year": 1974
  }
}

EDIT I'm not creating the json, with JSON.stringify(). My JSON is created by Form Builder module from Angular 2, despite the fact that I'm setting it's type to number, after I change the value, the value gets quotes.

Gerald Hughes
  • 5,771
  • 20
  • 73
  • 131
  • Possible duplicate of [JSON.stringify() values as numbers?](http://stackoverflow.com/questions/18302890/json-stringify-input-values-as-numbers) – Damaged Organic Sep 23 '16 at 09:38
  • Where is the data coming from? You would be better to build it correctly at source. Failing that, you could always use `parseFloat()` or `parseInt()`. – BenM Sep 23 '16 at 09:40
  • You're *getting* that JSON from somewhere? Or you're generating it yourself? Can the issue be fixed before encoding to JSON? Or do you have to work with this JSON and need the values as numbers internally? Are the specific properties which are expected to contain numbers known, or do you have to evaluate each value whether it should be a number? **This is not an answerable question as is!** – deceze Sep 23 '16 at 09:40
  • @deceze the json is generated by FormBuilder module from angular 2 – Gerald Hughes Sep 23 '16 at 09:42

4 Answers4

2

Before saving your JSON, use the parseInt() functions to convert your values into integers. This will remove the quotes.

JSON.stringify({
    Month: parseInt( value , 10);
})

See this answer

Community
  • 1
  • 1
Adrian Lambertz
  • 913
  • 6
  • 11
1

EDIT

If you made that JSON Object earlier in your JavaScript code, go for @Adrian Lambertz's answer. If you got that JSON as a String from somewhere else and want to convert it, read my answer :

My original answer

Say you got this JSON as a string in your JavaScript code, you could convert the desired values to integers like this :

var events = JSON.parse(JSONStringYouWantToConvert);

// if the JSON String is an array of events that all have a Title, a StartDate and an EndDate
for (var i = 0; i < events.length; i++) {
    // else, forget about the loop and the [i] index, the concept remains the same
    events[i].StartDate.Month = parseInt(events[i].StartDate.Month);
    events[i].StartDate.Year = parseInt(events[i].StartDate.Year);
    events[i].EndDate.Month = parseInt(events[i].EndDate.Month);
    events[i].EndDate.Year = parseInt(events[i].EndDate.Year);
}

// make a JSON String that wont have the quotes around the Month and Year numbers
var JSONStringConverted = JSON.stringify(events);
Mouradif
  • 2,666
  • 1
  • 20
  • 37
0

Just convert the strings to numbers. This code is just an example, you can adapt it to whatever your object structure is.

function normalize(target) {
  for (const date of ['StartDate', 'EndDate']) {
    for (const item of ['Month', 'Year']) {
      target[date][item] = Number(target[date][item]);
    }
  }
}
Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
0

I can not see the order from which data the result is expeceted, so here two versions.

Object -> JSON (-> Object)

This works with JSON.stringify and a replacer function for creating numbers for certain keys, like Month and Year.

var dataObj = { "Title": "Test", "StartDate": { "Month": "3", "Year": "1973" }, "EndDate": { "Month": "4", "Year": "1974" } },
    jsonStr = JSON.stringify(dataObj, function (k, v) {
        return ['Month', 'Year'].indexOf(k) !== -1 ? +v : v;
    }),
    parsed = JSON.parse(jsonStr);

console.log(jsonStr);
console.log(parsed);

JSON -> Object

This works with JSON.parse and a reviver function for creating numbers for certain keys, like Month and Year.

var jsonStr = '{ "Title": "Test", "StartDate": { "Month": "3", "Year": "1973" }, "EndDate": { "Month": "4", "Year": "1974" } }',
    parsed = JSON.parse(jsonStr, function (k, v) {
        return ['Month', 'Year'].indexOf(k) !== -1 ? +v : v;
    });

console.log(parsed);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392