Help

How to get info out of the JSON that an API delivered

Topic Labels: ImportingExporting
7194 10
cancel
Showing results for 
Search instead for 
Did you mean: 
Ron_Daniel
8 - Airtable Astronomer
8 - Airtable Astronomer

I’m a total newbie to JavaScript, and pretty proud of myself that I’ve figured out how to connect to my company’s API to get store information.

console.log(await storeInfo.json());
is displaying all of the JSON returned from the API call, but now how do I get that data out of the JSON?

Example:

CONSOLE.LOG

  1. :arrow_forward: (1) [Object]
  2. :arrow_forward: 0: Object
    1. StoreNumber: “1234”
    2. StoreName: “Joe’s Market”
    3. :arrow_forward: StorePhoneNumbers: Array(2)
    1. :arrow_forward: 0: Object
    2. Description: “Fax”
    3. PhoneNumber: “800-555-1212”
    4. :arrow_forward: 1: Object
    5. Description: “Landline”
    6. PhoneNumber: “800-555-2121”

How can I pull the store name and phone numbers out into individual variables that I can do stuff with?

10 Replies 10

In JS, in general, you work with objects and arrays. It’s not so easy to understand, but as soon as you do, you will understand JS ))
I’m not a pro developer, but sometimes it’s easier to explain by simple words.

Object is a data type, which have one or many ‘key:value’ pairs
in your example it’s “storenumber” : “1234”, “storename” : “joe’s market”
Value can be string, number (btw, “1234” is a string in your case), Object or Array

Array is a “box” of some values (strings, numbers or Objects or Arrays), usually similar type.

The same format Airtable uses for tables, fields etc info

paste that in new scripting window, put any of your table name, run and hope it will add some understanding ))

const mytable=base.getTable('PUT YOUR TABLE NAME HERE')
output.inspect(mytable)
output.text(mytable.name)
const c=mytable.id 
output.text(c)
const flds=mytable.fields // Fields is array of Field objects
flds.forEach(f=>{output.text(f.name);
    output.inspect(f) })   

output.text('Lets iterate object key-value pairs')
const d=Object.entries(flds[0])
output.inspect(d)

Thank you for responding. My question is not about Airtable’s formatting, it is about how to use the data in the JSON that the API returned.

  • API is called (accomplished)
  • JSON is returned (accomplished)
  • A key pair is pulled from the JSON (???)
    In other words, I want a variable called storeNumber with the value of 1234. How do I get that from the JSON?

The answer depends on where [exactly] you are capturing this data inside Airtable. Is your script running in an automation script? A webhook listener? An external scripting environment?

Please be specific about where your code is running such that it has access to this JSON data.

Bill, right now I am in the scripting app, but am open to doing this anywhere in Airtable. I am hitting our company’s API with this script (some info replaced with X’s for privacy):

let storeInfo = await remoteFetchAsync(‘https://api.XXXX.com/store-directory-get/detail?changesSince=2000-01-01&storeNumber=1234’, {
headers: {
‘Ocp-Apim-Subscription-Key’: ‘XXXXXXXXXXXXXXXXXXX’,
},
});
console.log(await storeInfo.json());

The console shows me the JSON, but I don’t know how to start using the info in it.

Okay - good to know.

@Alexey_Gusev is on the right track. I would test some values in your script to see what surfaces from the JSON object.

I would add these lines after the call to fetch storeInfo to see inside the object a little more clearly.

console.log(storeInfo);
console.log(JSON.stringify(storeInfo));

I would also experiment with a statement like this:

console.log('Store Name:' + storeInfo[0].storeName); // I assume this is an array of JSON objects

And if that works, you could graduate to listing all store names in the resulting payload:

for (var i in storeInfo)
{
  console.log('Store Name:' + storeInfo[i].storeName); 
}
console.log(storeInfo); //this one gave me the url, status, etc. info about the call
console.log(JSON.stringify(storeInfo)); //this one returned just "{}"
console.log('Store Name:' + storeInfo[0].storeName); //this one errors: Cannot read properties of undefined (reading 'storeName')

The example that you provide does not look like a screen capture. An actual screen capture would be helpful because sometimes the forums changes the formatting when you simply copy/paste in text.

console.log(await storeInfo.json());

It looks like you are getting the information and logging it to the console, without actually saving the data to a variable for future use. Instead, save the result to a variable like this:

const data = await storeInfo.json()
console.log(data)

Then, as Alexey says, you need to understand JavaScript objects and arrays in order to access the individual parts of the data.

const data = await storeInfo.json()
console.log(data)

const storeNumber = data["StoreNumber"]
console.log(storeNumber)

const storeName = data["StoreName"]
console.log(storeName)

const arrayOfPhoneNumberObjects = data["StorePhoneNumbers"]
console.log(arrayOfPhoneNumberObjects )

const firstPhoneNumberObject = arrayOfPhoneNumberObjects [0]
console.log(firstPhoneNumberObject )

const firstPhoneNumber = firstPhoneNumberObject["PhoneNumber"]
console.log(firstPhoneNumber )

Kuovonne, thank you - this is starting to make sense. In your example, you can see in my screencap that storeName is returning as undefined. (I’m wondering if the mistake is that I need to be more specific when pulling “StoreName” out - do I need to be specifying that it is in Object 0?)
Screen Shot 2022-02-06 at 1.52.33 PM

Kuovonne, I got it! Thank you!
Screen Shot 2022-02-06 at 2.27.38 PM