-1

Hello all I have a question.

With this code:

targetingIdeaService.get({selector: selector}, function (error, result) {

var string = JSON.stringify(result)
console.log(string)

})

I get this result:

"totalNumEntries":700,
"entries":[
{
"data":[
{
"key":"KEYWORD_TEXT",
"value":{
"attributes":{
"xsi:type":"StringAttribute"
},
"Attribute.Type":"StringAttribute",
"value":"nike ddd8ea95"
}
},
{
"key":"COMPETITION",
"value":{
"attributes":{
"xsi:type":"DoubleAttribute"
},
"Attribute.Type":" DoubleAttribute",
"value":"0.8726547440705715"
}
},
{
"key":"AVERAGE_CPC",
"value":{
"attributes":{
"xsi:type":"MoneyAttribute"
},
"Attribute.Type":"MoneyAttribute",
"value":{
"ComparableValue.Type":"Money",
"microAmount":"16769286"
}
}
},
{
"key":"SEARCH_VOLUME",
"value":{
"attributes":{
"x si:type":"LongAttribute"
},
"Attribute.Type":"LongAttribute",
"value":"5609289"
}
}
]
}
]
}

And with this one i get the following:

targetingIdeaService.get({selector: selector}, function (error, result) {


var resultaten = result;
var res = resultaten.entries;
for(var i = 0; i < res.length; i++){
console.log(resultaten.entries[i])
 }



})

Output

{ data:
   [ { key: 'KEYWORD_TEXT', value: [Object] },
     { key: 'COMPETITION', value: [Object] },
     { key: 'AVERAGE_CPC', value: [Object] },
     { key: 'SEARCH_VOLUME', value: [Object] } ] }

Now im looking to format the JSON a certain way, It has to look like this example. Notice: the key value pairs of json data.

[
  {
    "KEYWORD_TEXT": "red herring 9e23f4ad",
    "SEARCH_VOLUME": 4574730
  },
  {
    "KEYWORD_TEXT": "nike 656e95f0",
    "SEARCH_VOLUME": 3442386
  },
  etc...
]

Basically the Key and the value of that key next to eachother. How to do this?

Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • This is not available out of the box, you should search for a library that does it – smnbbrv Jul 21 '17 at 11:58
  • Possible duplicate of [How can I pretty-print JSON using JavaScript?](https://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript) – Tobias Meister Jul 21 '17 at 11:59
  • This has nothing to do with JSON. You're just taking a data structure and creating a different data structure based on it. – Quentin Jul 21 '17 at 11:59

1 Answers1

1

You can map the keys to values like following -

resultaten = result.data.map(function (item) {
  return {
    [item.key]: item.value
  }
});

JSON.stringify(resultaten);
shawon191
  • 1,945
  • 13
  • 28
  • Hi Thanks for your answer, but the above code gives me result.map is not a function – Kevin.a Jul 21 '17 at 12:30
  • @Kevin.a, sorry, my bad, since `result` returns a object with an array in it's `data` property, you need to run `result.data.map`. – shawon191 Jul 21 '17 at 12:35
  • Thanks for your quick comment, I now get TypeError: Cannot read property 'map' of undefined – Kevin.a Jul 21 '17 at 12:38
  • @Kevin.a make sure you have `data` property in your `result`. – shawon191 Jul 21 '17 at 12:58
  • No, my code is assuming the result object you're getting from the callback of your service has a `data` property. You need to either make sure there is data property in `result` object or add conditional for situations where there is no data object. – shawon191 Jul 21 '17 at 13:18