0

Is it possible to get list of attributes of a particular JSON map with a JSONPath expression? For example:

{"foo": 1, "bar": 2} => ["foo","bar"]

flowjow
  • 171
  • 1
  • 3
  • 14
  • Here's a [link](http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object) that may help you. – M. A. Cordeiro Jan 25 '17 at 13:34

2 Answers2

2

You can use the following code:

Object.getOwnPropertyNames({"foo": 1, "bar": 2})

Here's a link for further information.

M. A. Cordeiro
  • 469
  • 8
  • 14
  • 1
    English link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames – Bruce Oct 26 '21 at 06:44
1

Loop through each json object get all the keys and push then into an array. You can try this:

var data=[{"foo": 1, "bar": 2},{"foo1": 1, "bar1": 2}];
var array=[];
$.each(data, function(key, value){
  var item;
   item=Object.keys(value);
  array.push(item);
});

console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44