0

Okay, so im trying to print the trackName in this json object: https://pastebin.com/raw/KfEDtRPY. but i just cant seem to find out the right properties. i tried doing: console.log(res.text.results[0].trackName); but it just gives me this error: TypeError: Cannot read property '0' of undefined and by the way, to get that json object i did console.log(res.text); i just cant seem to find the correct way to print JUST trackName. Thanks in advance.

NOTE: I'm using superagent if that's any help.

  • `obj.results[0].trackName` **is** the correct path -> https://jsfiddle.net/adeneo/25m8vv5r/ – adeneo Jul 21 '17 at 22:58
  • I still get the same error though? – SpecialVirusTasker Jul 21 '17 at 23:08
  • @SpecialVirusTasker Please mark my answer as accepted answer if it has resolved your need, – user2347763 Jul 22 '17 at 01:22
  • i'd be glad to, but it doesnt work quite correctly. – SpecialVirusTasker Jul 22 '17 at 03:27
  • This question has been toying around for some time already. Please reduce the JSON file down to a readable sample (i.e. 2 or 3 rows) then put that sample directly into the question. A tip for debugging is to always put data issues out of the way if you want to debug functionalities. Then, put functionality issues out of the way if you want to debug data. – Frederik.L Jul 22 '17 at 16:14

3 Answers3

2

typeof res.text is string, first parse it with the help of JSON.parse(res.text) and then access it.

Kaps
  • 189
  • 7
1

do it like this, try this:

var jsonString = Json.parse(res.text)
console.log(jsonString.results[0].trackName)
Rahul Beniwal
  • 639
  • 4
  • 9
1
  1. Your JSON file was incomplete (test threw an unexpected end of JSON input error). Or, the other possibility being that your code timed out so increase the timeout value of your callback to readfile (async) (if you are using it)

Use the below code, it works, tested.

    var chai = require('chai'),expect = chai.expect,fs=require('fs');describe('Verifying a JSON value', function () { it('checks if tackname of index 0 is not undefined', function () {
                        var data=fs.readFileSync('./test/trex.json');
                        var jsonValue=JSON.parse(data);
                        var x=jsonValue.results[0].trackName;
                        console.log("jsonvalue "+x);                        
                        expect(x).equals("2U (feat. Justin Bieber)");           
    });
});

Mocha test result

user2347763
  • 469
  • 2
  • 10
  • hmm i seem to be getting the `ReferenceError: describe is not defined` error. and i'd love it if i dont have to require anything – SpecialVirusTasker Jul 22 '17 at 03:24
  • Assuming that you are using node.js and mocha installed as "npm install mocha --save-dev" and then running the "testfile.js"(that contains the describe) on node with "mocha testfile" from the folder where you have both mocha and testfile - please refer to this link - https://stackoverflow.com/questions/28400459/referenceerror-describe-is-not-defined-nodejs – user2347763 Jul 22 '17 at 03:29
  • erm im not using mocha? im using superagent – SpecialVirusTasker Jul 22 '17 at 03:33
  • Surely, you can just use this part of code whereever you are reading the file? var data=fs.readFileSync('./test/trex.json'); // Change the path to point to the location of your json file var jsonValue=JSON.parse(data); var x=jsonValue.results[0].trackName; console.log("jsonvalue "+x); – user2347763 Jul 22 '17 at 09:37
  • i actually found the solution, its much shorter. but i cant thank you enough for your time – SpecialVirusTasker Jul 22 '17 at 16:15