Home > Mobile >  How to convert raw github file data to JSON
How to convert raw github file data to JSON

Time:11-15

I am trying to convert github raw data to JSON but unfortunately I am not able to do that. I am using reactJs My Code:

fetch("https://api.github.com/repos/graphql-compose/graphql-compose-examples/contents/examples/northwind/data/csv/employees.csv")
          .then((res) => {
            if (res.ok) {
              return res.content; // need to convert this content to json
            } else {
              throw new Error("Something went wrong");
            }
          })
          .then((data) => console.log(data))
          .catch((error) => {
            console.log(error);
          });

CodePudding user response:

You can call json() on the response. See "basic fetch request" example here.

The content property contains a base64 encoded value but it also contains line breaks which have to be removed before you're able to convert using atob. Afterwards, you get CSV lines:

fetch("https://api.github.com/repos/graphql-compose/graphql-compose-examples/contents/examples/northwind/data/csv/employees.csv")
    .then(res => res.json())
    .then(data => console.log(atob(data.content.replace('\n', ''))))

Output:

employeeID,lastName,firstName,title,titleOfCourtesy,birthDate,hireDate,address,city,region,postalCode,country,homePhone,extension,photo,notes,reportsTo,photoPath
1,Davolio,Nancy,Sales Representative,Ms.,1948-12-08 00:00:00.000,1992-05-01 00:00:00.000,507 20th Ave. E. Apt. 2A,Seattle,WA,98122,USA,(206) 555-9857,5467,0x151C2F00020000000D000E0014002100FFFFFFFF4269746D617020496D616765005061696E742E506963747572650001050000020000000700000050427275736800000000000000000020540000424D20540000000000007600000028000000C0000000DF0000000100040000000000A0530000CE0E0000D80E0000000000,Education includes a BA in psychology from Colorado State University in 1970.  She also completed The Art of the Cold Call.  Nancy is a member of Toastmasters International.,2,http://accweb/emmployees/davolio.bmp
...
  • Related