Need to convert json key-value pairs to standard array
Solution 1
Once you've deserialized the data (e.g., you have myData
, which is an object), you can loop through its keys using for..in
, and then build up an array that combines keys and values:
var myData, dataArray, key;
myData = $.parse(JSON(data));
dataArray = [];
for (key in myData) {
dataArray.push(key); // Push the key on the array
dataArray.push(myData[key]); // Push the key's value on the array
}
Since myData
is the result of deserializing the JSON in data
, we know that myData
is a generic object (e.g., just a {}
as opposed to a new Foo
or something like that), so we don't even need hasOwnProperty
. If we didn't know that, and we only wanted to enumerate myData
's own keys and values, we would add a hasOwnProperty
check:
var myData, dataArray, key;
myData = $.parse(JSON(data));
dataArray = [];
for (key in myData) {
if (myData.hasOwnProperty(key)) {
dataArray.push(key); // Push the key on the array
dataArray.push(myData[key]); // Push the key's value on the array
}
}
There's no reason to do that in your case, unless someone has been mucking about with Object.prototype
(in which case, take them behind the woodshed, give them a severe hiding, and then have them write "I will not muck about with Object.prototype
several hundred times on the chalkboard), but whenever you use for..in
, it's always good to stop and think whether A) The object is guaranteed to be vanilla, and B) If not, do you want only its own properties, or do you also want ones it inherits?
Solution 2
var data = $.parse(JSON({"feat_3":"4356","feat_4":"45","feat_5":"564","feat_6":"7566"}));
var arr = [];
for( var i in data ) {
if( data.hasOwnProperty( i ) ){
arr.push( i, data[i] );
}
}
Array will be :
["feat_3", "4356", "feat_4", "45", "feat_5", "564", "feat_6", "7566"]
Radley Sustaire
I'm a web developer specializing in WordPress theme and plugin development. I have more than 12 years of experience in the industry and own my own business, ZingMap, which all of my services revolve around WordPress. I often do freelance work for small business and contracts for agencies such as Alchemy + Aim. I am very familiar with popular plugins like WooCommerce, Advanced Custom Fields, Gravity Forms and much more. I'm very familiar with the database structure and managing queries within WordPress and do this sort of backend work on a daily basis.
Updated on June 04, 2022Comments
-
Radley Sustaire almost 2 years
I'm interperting a json string into a variable using jquery's parseJSON() function. The problem is, it's turning my data into an object instead of a 2d array. Eg,
myData = $.parse(JSON(data)); myData.name// = "Bob"
The problem is, "name" is not supposed to be a key (assuming that is the correct term). Instead, it should be:
myData[0] // = "name" myData[1] // = "Bob"
How would I convert this? Or is there a different method than using a for loop to walk through the index of an array (but still be able to access both key and value as a string, as you would in a 2d array).
EDIT: This is some json that is in use (Note it's MUCH longer). This is what is given for "data"
{"feat_3":"4356","feat_4":"45","feat_5":"564","feat_6":"7566"}
-
Teun Pronk about 12 yearsCould you show the code where you fill
data
-
Radley Sustaire about 12 yearsAdded a snippet of json, is that what you were looking for?
-
T.J. Crowder about 12 yearsIt seems a bit of an odd thing to do, if you don't mind my saying. Why create a flat array with both the keys and their values?
-
Teun Pronk about 12 yearswhere is bob then? because it would be like feat_3 could be bob instead of 4356
-
Radley Sustaire about 12 yearsYeah, I used your example rather than moving it to a "flat array" which makes much more sense.
-
Radley Sustaire about 12 years@Teun, in that example bob it would have been {"name":"Bob"}
-
-
Radley Sustaire about 12 yearsThe first code block worked golden. The way it works out, I don't even need to move them to a new array. I can just assign the data in that loop itself. Wonderful! Thanks
-
Radley Sustaire about 12 yearsThanks, but I found TJ's solution to be a better solution than converting to 2d arrays like I had originally planned.
-
abuduba about 12 yearsCorrected. Into
push
method you can apply more than one argument which will filled out an array