1

I get a JSON response like this from a server,

{ id : 1, text : 'Name City Country \nJohn \n Chicago \nIllinois \nAlbert \nHouston \nTexas ' }

if I do console.log(response.text); the output is like this in tabular form

Table Image 1

Now I want only want the Name column along with the rows and the output should look like this

Table Image 2

Suggest me a workaround for this. Since the value is a string I'm facing a lot of trouble to extract only the required columns

node_man
  • 1,359
  • 4
  • 23
  • 50

3 Answers3

1

I haven't tested this, but something like this should work:

var jsonData = "{ id : 1, text : 'Name City Country \nJohn \n Chicago \nIllinois \nAlbert \nHouston \nTexas ' }";

var obj1 = jsonData.split('\n');

for(var i= 1; i<obj1.length-2; i=i+3) 
{
  console.log("Name: " +obj1[i]+", City: "+obj1[i + 1]+", Country: " +obj1[i + 2]);

}
Praneet Nadkar
  • 823
  • 7
  • 16
0

Use trim and split to get the Name City and Country texts.

const obj = { id : 1, text : 'Name City Country \nJohn \n Chicago \nIllinois \nAlbert \nHouston \nTexas ' }

console.log(obj.text.split("\n")[0].trim().split(" "));
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
0

You can use any of the javascript csv parser to parse the text to csv and access the particular column.

Ref: javascript-code-to-parse-csv-data