Solved: How to parse response.body in sync table

Greetings

I am working on a pack with a sync table from an API.

I believe with the help of the excellent Coda documentation I have achieved the hardest part and can retrieve the data I am seeking, as I can display response.body as a console.log message in Pack Maker tools as below:

The problem is that the data does not get assigned to columns in my table with a simple “return response.body.” I cannot even log individual fields.

I believe this is a complex JSON object correct? Let’s suppose I want to retrieve the field title. If I write to log response.body.title it says ‘undefined.’

I know I need to write a function for each column. How do I write a function for example that takes each response.body.title and response.body.abstractText and places those in the corresponding columns in my new table?

[ { key: 'S9BTJNAD', version: 20563, library: { type: 'user', id: 571747, name: 'rkaplan@umrpc.com', links: [Object] }, links: { self: [Object], alternate: [Object], up: [Object] }, meta: {}, data: { key: 'S9BTJNAD', version: 20563, parentItem: 'KVNKCAXQ', itemType: 'attachment', linkMode: 'linked_url', title: 'PubMed entry', accessDate: '2022-04-14T17:09:18Z', url: 'http://www.ncbi.nlm.nih.gov/pubmed/32702693', note: '', contentType: 'text/html', charset: '', tags: [], relations: {}, dateAdded: '2022-04-14T17:09:18Z', dateModified: '2022-04-14T17:09:18Z' } }, { key: 'KVNKCAXQ', version: 20563, library: { type: 'user', id: 571747, name: 'rkaplan@umrpc.com', links: [Object] }, links: { self: [Object], alternate: [Object] }, meta: { creatorSummary: 'Vajramani', parsedDate: '2020', numChildren: 1 }, data: { key: 'KVNKCAXQ', version: 20563, itemType: 'journalArticle', title: 'Percutaneous Electrical Nerve Stimulation for Facial Pain', creators: [Object], abstractNote: 'Percutaneous electrical nerve stimulation (PENS) is a novel, minimally invasive

To clarify above -

The above output comes from the following:

 var citations=response.body;
 console.log(citations);

and as you can see the resulting data begins with:

[ { key: 'S9BTJNAD', version: 20563, library: { type: 'user', id: 571747, name: 'rkaplan@umrpc.com', links: [Object] }, links: { self: [Object], alternate: [Object], up: [Object] }, meta: {}, data: { key: 'S9BTJNAD',

Thinking this is a JSON object I tried:

var obj = JSON.parse(citations);
console.log(obj);

This gives me the error

"Unexpected token o in JSON at position 1"

which leads me to conclude that the JSON is already parsed.

So instead I tried

 var firstkey=citations.key;
 console.log(firstkey);

as well as

var firstkey=citations.key[0];
console.log(firstkey);

and I get the response undefined or cannot read property 0 of undefined

What am I missing here in terms of how to parse this API response further?

Solved… problem was simply not addressing the object correctly.

It was citations[0].key rather than citations.key[0].

1 Like