0

Solved my problem

var allKeys = ["key","en","ar"];
for(var i=0;i<allKeys.length;i++) {
  for(j=0;j<jsonText.Sheet1.length;j++) { 
   console.log(allKeys[i] + ' - ' + jsonText.Sheet1[j][allKeys[i]]);
  }
}

Running version

Thanks @Aniket Sinha


How I can parse and then group incorrect JSON data?

My incorrect JSON data;

{
  "Sheet1": [
    {
      "key": "title",
      "en": "title",
      "ar": "arabic_title", //trailing comma here
    },
    {
      "key": "content",
      "en": "content",
      "ar": "arabic_content",  //trailing comma here
    }
  ]
}

I want this result:

key - title

key - content

en - title

en - content

ar - arabic_title

ar - arabic_content

Aniket Sinha
  • 6,001
  • 6
  • 37
  • 50
secret35
  • 56
  • 7
  • What's the problem you're facing? Are you unable to fetch the values from this JSON? Or do you want to group all the keys `key`, `en` and `ar` together? – Aniket Sinha Nov 13 '16 at 21:23
  • Yeah! I want to group all the keys key, en and ar together. – secret35 Nov 13 '16 at 21:31
  • @secret35 please check my answer – Erik Cupal Nov 13 '16 at 21:32
  • The JSON provided is still invalid. There can be no comma after the last value in an object literal. Please explain where you get this invalid formatted data from. – trincot Nov 13 '16 at 21:34
  • @trincot I convert excel to json. I use this lib : https://github.com/SheetJS/js-xlsx/blob/master/README.md - this lib result data above – secret35 Nov 13 '16 at 21:38
  • @lapuckire I read your answer because my library gives a result like this. – secret35 Nov 13 '16 at 21:40
  • If that is indeed true, and you are not building the JSON yourself via calls to that library, then contact the author of the library and explain your issue. As long as the JSON is not correct, you cannot use JSON.parse. – trincot Nov 13 '16 at 21:48
  • 2
    Since this question has been marked as duplicate, I'm unable to answer it. Check this plunk (https://plnkr.co/edit/BN1cb9LGuC0uTkkKDhtT?p=preview) for solution. Check console for output. – Aniket Sinha Nov 13 '16 at 21:51
  • Regarding the JSON, it's invalid because of dangling comma after `"ar": "arabic_title",`. Read up on it here: http://eslint.org/docs/rules/no-comma-dangle – Aniket Sinha Nov 13 '16 at 21:52
  • @AniketSinha thank you so much for reply, It's worked – secret35 Nov 13 '16 at 21:55
  • @AniketSinha I updated json data but libs the last line adds a comma :( – secret35 Nov 13 '16 at 22:08
  • It's unfortunate that the lib is generating trailing comma. In that case, hackish way is to remove all trailing commas using `replace`. `JSON.parse(jsonText.replace(/,}/g,'}'))`, where `jsonText` is your string having trailing comma. Plunk for reference: https://plnkr.co/edit/xgFDZgUt3TTOr8VdkySp?p=preview – Aniket Sinha Nov 14 '16 at 11:47

1 Answers1

0

JSON.parse()

var object = JSON.parse('{"Sheet1":[{"key":"title","en":"title","ar":"arabic_title"},{"key":"content","en":"content","ar":"arabic_content"}]}');
var key0 = object.Sheet1[0].key;
key0 == "title";

ADDITION:

Your JSON string is in bad format. There should by no comma after "ar": "arabic_title" and "ar": "arabic_content".

Erik Cupal
  • 2,735
  • 1
  • 19
  • 20