0

My data currently looks like this:

{ 'Australia' : [ Array[2] ],
  'Bangladesh' : [ Array[7] ],
  etc...}

I could like to convert it to:

[ { 'country': 'Australia',
    'count': 2 },
  { 'country' : 'Bangladesh',
    'count': 7},
  etc...
 ]

What is the easiest way to convert the data?

1
  • what do you mean by that?, Wriging converter should't be so hard. Decode this data to an object and later map this object with gson or jacskon or any other JSON mapper to desired format. If you want to convert only one sample of data maybe some specialized textEditor(Sublime, Notepad++ etc) could be helpful. Most of IDE from InteliJ have also such as possibilities Commented Nov 14, 2016 at 14:08

2 Answers 2

2

Using a library like Underscore, you could map the object into the form you're trying to generate:

_.map(_.keys(data), function(key) {
    return { 'country': key, 'count': data[key].length }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Using underscore you may use something like this

var cities = {"Australia" : ["Sydney, Perth"], "US" : ["New York", "Boston"]}
var list = [];
_.each(_.keys(cities), function(countryName){
	list.push({"country" : countryName, "count" : cities[countryName].length})
});
console.log(list)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

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.