0

I have a little problem with javascript here. I've been searching but with no luck. So I have the following json object:

{
  core: {
      repositoryformatversion: '0',
      filemode: 'true',
      bare: 'false',
      logallrefupdates: 'true'
  },
  'remote "origin"': {
      fetch: '+refs/heads/*:refs/remotes/origin/*',
      url: 'https://github.com/user/repo.git'
  },
  'branch "master"': {
      remote: 'origin',
      merge: 'refs/heads/master'
  }
}

and here is my script:

var iniparser = require('iniparser');
var result = [];

iniparser.parse('analytics.js/.git/config', function(err,data){
   result.push(data);
});

console.log(result);

it returns []. Actually, I want to push just the url (https://github.com/user/repo.git) by using result.push(data['remote "origin"'].url). when I use console.log(data['remote "origin"'].url) it returns the url correctly.

Thank you.

rizqyhi
  • 70
  • 1
  • 8
  • 1
    Your JSON is invalid. http://jsonlint.com/ – Kevin Boucher Jun 01 '13 at 18:58
  • Related: http://stackoverflow.com/q/14220321/. Not an exact match, but you're experiencing the same problem just from different tasks. – Jonathan Lonowski Jun 01 '13 at 19:10
  • @KevinBoucher but when I log that, it returns correctly. or any other suggestion? – rizqyhi Jun 01 '13 at 19:12
  • @rizqy22 `iniparser` may be more lenient in its parsing than [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) and [similar](http://json.org/). But, you should plan for stricter parsing and make sure your JSON is valid. Including: all object keys must be strings and all strings are double-quoted: `{ "core": { ... }, "remote \"origin\"": { ... }, ... }`. – Jonathan Lonowski Jun 01 '13 at 19:15

1 Answers1

1

are you using none-iniparser?

if so it looks like parse is asyncronous (i'm judging by the callback, and that there is a parseSync function.

This means that you dont really know when the callback will be invoked. parse is called ,then your program instantly moves to console.log the result, which hasn't changed yet. THEN at some time, whenever parse is finished, the parse callback is invoked

dm03514
  • 54,664
  • 18
  • 108
  • 145