2

At the moment I am stuck with a problem that just seems stupid, but I don't know the answer to it.

I am trying to access this JSON-object:

 var custom_fields = 
 {
   "28246": 5123,5124,5125
 }

I would like to get each value from that key. I would know how to access it if it was a nested-object, but it isn't sadly (it is coming from an API, which I can't change the JSON-response from sadly)

What I tried already is the following:

for (var key in custom_fields) {
   if (custom_fields.hasOwnProperty(key)) {
      console.log(key + " -> " + custom_fields[key]);
    }
}

The problem here is that the result will be like this:

1 -> 5
2 -> 1
3 -> 2
4 -> 3
5 -> ,
6 -> 5
...etc...

Any suggestions are welcome, I am trying to access it in javascript/Jquery.

Thanks for helping in advance!

5
  • 1
    It's just not a correct json Commented Oct 27, 2017 at 12:00
  • @Faly I thought so too, but there has to be an alternative way, for example concatenating the results separated by the comma's. Would you know how? Commented Oct 27, 2017 at 12:03
  • If the part 5123,5124,5125 is a string like "5123,5124,5125", we can do something Commented Oct 27, 2017 at 12:04
  • Is your variable 'custom_fields' an accurate example of the JSON received from the API? The three integers should be an array. Commented Oct 27, 2017 at 12:14
  • @Faly yes it was, little mistake there sorry. Thanks for helping anyways Commented Oct 27, 2017 at 12:16

3 Answers 3

3

I assume that the data is in this format (note the string literals):

var custom_fields = { 
   "28246": "5123,5124,5125" 
}

If that is the case, you can use String.split. In your case, it would be something like this:

const values = custom_fields['28246'].split(',');

The values of they key 28246 are now stored in the new variable values as an array:

['5123','5124','5125']

If you want to parse all values to integers, I suggest using Array.map:

const valuesAsInt = custom_fields['28246'].split(',').map(value => parseInt(value);

Which will lead to this:

[5123, 5124, 5125]

Disclaimer: When using newer ECMAScript features such as Array.map, be sure to either use a browser which supports this our include a polyfill.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, I am ashamed that I forgot about the .split() method. Anyways, should I report this as a mistake in the API, what do you think?
If the list of values is not wrapped in string literals, it is not valid JSON (you can easily check with tools like jsonlint.com) and therefore definitely a mistake in the API.
Hi I have a similar issues but i have strings as vales e.g ['yes, allowed', 'no, not allowed']. The issue is my values also contain commas and split just splits the individual values too. Any ideas how can i avoid it? Thank you @glutengo
0

You can access it by using split function which will convert it into an array and then get the values from that array as below code.

var data = {
  "28246": '5123,5124,5125'
}
var arr = data['28246'].split(',');
$.each(arr, function( index, value ) {
  console.log(value);
});

1 Comment

Thanks for the reply, I would accept if i could accept 2 answers, because this one is correct as well
0

You can split by ',' and transform each element to integer by using array.map and '+' operator:

 var custom_fields = 
 {
   "28246": "5123,5124,5125"
 }

custom_fields["28246"] = custom_fields["28246"].split(',').map(el => +el);
console.log(custom_fields);
console.log(custom_fields["28246"][0], custom_fields["28246"][1], custom_fields["28246"][2]);

Comments

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.