2

I have this:

var a = {};
a[1] = 1;
a[4] = 4;
console.log(JSON.stringify(a));

then I get:

{"1":1,"4":4}

but I want to get:

{1:1,4:4}

how to reach this? In other words, I want to keys be real int.

7
  • 5
    Object keys are always strings. No such thing exists. Commented Jun 27, 2016 at 22:09
  • 2
    ... and thus that last code example is invalid JSON. Commented Jun 27, 2016 at 22:09
  • 2
    If it's useful, you can declare a as an array ([]) and get back [null,1,null,null,4] after JSONification - which when parsed pack will give the expected values at those indices. Commented Jun 27, 2016 at 22:10
  • 1
    What is the purpose of why you need that format? Do you want to send to the server that way, are you trying reference them a specific way later? Commented Jun 27, 2016 at 22:20
  • 1
    Looks like some serverside handmade json parser issue :) Commented Jun 27, 2016 at 22:22

3 Answers 3

3

When you call JSON.stringify() method it creates a valid JSON string. One of the rules for valid JSON is that every property should be in "quotes".

So thats why it is impossible to get such result as you want using JSON.stringify.

If you want to just convert such object to array it is possible, for example usin such function.

function numerableObjectToArr(obj) {
  var result = [];
  var keys = Object.keys(obj);
  keys.forEach(function(item){
    result.push(obj[item]);
  })
  return result; 
}

var a = {};
a[1] = 1;
a[4] = 4;
numerableObjectToArr(a); // returns [1, 4]

But in this way you will just receive Array with values of existing properties in the obj.

But if your prop name means the index in the array, and you are sure that there will be always number as a prop name - you can improve this function:

function numerableObjectToArr(obj) {
  var result = [];
  var keys = Object.keys(obj);
  keys.forEach(function(item){
    result[+item] = obj[item];  //we put index, then we put value to that place in array
  })
  return result; 
}

var a = {};
a[1] = 1;
a[4] = 4;
numerableObjectToArr(a);  // returns [undefined, 1, undefined, undefined, 4]
Sign up to request clarification or add additional context in comments.

1 Comment

yea, I see, I realized that its a server-side issue: stackoverflow.com/questions/3445953/…
1

I'm not sure you can do what you're trying to do the as the keys have to be string values. I'd advise having string name for your keys (i.e 1 = One, 2 = Two, etc). You could then try this:

 var a = {};
    a.one = 1;
    a.two = 2;
    a.three = 3;
    a.four = 4;

    console.log(JSON.stringify(a));

I hope this helps.

Comments

-1

var a = {};
a[1] = 1;
a[4] = 4;
alert(JSON.stringify(a).replace(/\"([0-9]+)\":/g, '$1:'));

But it is kludge. JSON - has a string keys.

5 Comments

You can't parse JSON with regex.
it is not parsing. It is converting.
Yes, that's the point. To do it properly, you need to parse it, and then use a custom stringifier. You don't want Zalgo to come, do you?
I dont see any troubles to use regexps for replacing in JSON. But replacings in JSON - it is bad by itself.
I value this help, though Im not keen for raping JSON either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.