2

I have a JSON, from that I need to extract all the keys in the JSON. so far I extracted all keys, But I need to from multidimensional array with only key values.

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": {
        "submain": "Drizzle"
      },
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ]
}

I need to construct mulit-dimensional array something like this...

["coord",["lon","lat"],"weather",[["id","main","description",["small_descript","large_description"],"icon"]....] 
code that I had used is...

var keyValue1= [];
function keyList(obj) 
{
	Object.keys(obj).forEach(function(key) {
    	keyValue1.push(key);
	    if(typeof(obj[key]) == 'object')
	    {
	    	keyList(obj[key]);
	    } 
	});
	
}


var obj = {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":{"small_descript":"light intensity drizzle","large_descript":"light intensity drizzle"},"icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200};
keyList(obj);
console.log(keyValue1);

0

2 Answers 2

1

You can try this snippet (it's pretty self-explanatory)

let getKeyList = obj => Object.keys(obj).reduce((s, key) => {
  if (Array.isArray(obj[key])) {
    // if it's an array then reduce all it's values (keys) into an array of arrays...
    s.push(obj[key].reduce((acc, sub) => {
      acc.push(getKeyList(sub));
      return acc;
    }, []))
  } else {
    // if it's an object then recurse...
    if (typeof(obj[key]) === "object")
      s.push(getKeyList(obj[key]))
  }
  // always push the key
  s.push(key)
  return s;
}, [ /* store */ ]);


// the JSON data
var x = {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":{"small_descript":"light intensity drizzle","large_descript":"light intensity drizzle"},"icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200};

// then call it as
console.log(getKeyList(x));

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

Comments

0

You can use reduce with some recurently reading fields that are objects::

function keyList(obj) {
	return Object.keys(obj).reduce(function(arr, key) {
    arr.push(key);
    if(typeof(obj[key]) === 'object') {
      arr.push(keyList(obj[key]));
    }
    return arr;
	}, []);
	
}


var obj = {
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": "Drizzle",
      "description": {
        "small_descript": "light intensity drizzle",
        "large_descript": "light intensity drizzle"
      },
      "icon": "09d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 280.32,
    "pressure": 1012,
    "humidity": 81,
    "temp": {
      "min": 279.15,
      "max": 281.15
    }
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.1,
    "deg": 80
  },
  "clouds": {
    "all": 90
  },
  "dt": 1485789600,
  "sys": {
    "type": 1,
    "id": 5091,
    "message": 0.0103,
    "country": "GB",
    "sunrise": 1485762037,
    "sunset": 1485794875
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}
var keyValue1 = keyList(obj);
console.log(keyValue1);

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.