Automobile VIN Lookup Script

Viewing 7 reply threads
  • Author
    Posts
  • July 21, 2019 at 11:51 AM #36003

    Brendan
    Keymaster

    Yesterday I wrote a script for a customer that looks up vehicle information when given a VIN. It was originally based upon the movie details lookup script I wrote that’s in the Scripting topic of the online user manual.

    You would of course use your own field IDs for your own form, but I’ll also attach a .tff file that has this script built-in to it that you can import into your own database documents.

    var make_id = 'fld-e0d7613414694a53b9988e99ae238b75';
    var model_id = 'fld-d7cc5f3b431e4e7ba1183597e2823606';
    var year_id = 'fld-20634b8ce9a747e69c6522c07e95da8b';
    var vin_id = 'fld-73b7977d2c9f40e399e3fe138e63fa2e';
    
    function fetchVINData() {
    	var vin = record.getFieldValue(vin_id);
    	console.log(vin);
    	var url = 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevinvaluesextended/' + vin + '?format=json';
    	var vin_data = Utils.getJsonFromUrl(url);
    	return vin_data;
    }
    
    var vin_data = fetchVINData();
    var results = vin_data['Results'];
    
    if (results && results.length > 0) {
    	var vehicle_data = results[0];
    	var make = vehicle_data['Make'];
    	var model = vehicle_data['Model'];
    	var year = vehicle_data['ModelYear'];
    	record.setFieldValue(make_id, make);
    	record.setFieldValue(model_id, model);
    	record.setFieldValue(year_id, year);
    	form.saveAllChanges();
    }
    
    Attachments:
    You must be logged in to view attached files.
    August 27, 2019 at 3:27 PM #36543

    ben wise
    Participant

    wow, I could use this. how do i incorporate this into a form….

    August 27, 2019 at 8:28 PM #36555

    Sam Moffatt
    Participant

    You can load the attachment up into your existing document, alternatively you can get this script, create a new form script and update the field ID’s at the top to match your field IDs. On Tap Forms 5.3 or later, form scripts are a third tab next to form and field settings.

    August 29, 2019 at 4:35 PM #36594

    Mark
    Participant

    Hi – I’m trying to copy your approach on this VIN script to pull book details from Google Books API, but the data I can’t get the resulting data to parse the way this script works with the NHTSA data.

    here’s a sample URL
    https://www.googleapis.com/books/v1/volumes?q=isbn:9780061143397

    Where you refer to ‘Results’ in the NHTSA data, I’ve tried ‘item’ ‘kind’ etc. but it does not seem to recognize the data structure.

    I have a feeling I’m missing some detail about how to access the data’s structure.

    Thanks for any insight,
    Regards,
    Mark

    August 29, 2019 at 6:25 PM #36599

    Brendan
    Keymaster

    @Mark, can you post your script?

    That’s a JSON result, so it should work to parse as long as you parse it right. Note that items is an array, so you’d have to get that and then loop through those items and pick out any data you need.

    Basically it’s just recognizing what returned elements represent an array [ ... ] and what elements represent a dictionary { ... } and get the values appropriately.

    August 29, 2019 at 8:07 PM #36602

    Mark
    Participant

    Thanks for your response – it helped me realize that I don’t know how to parse through a JSON array and/or nested arrays.

    In playing with my script, I seem to be able to read one character at a time.

    I’ve tried looking at some tutorials, etc. but they seem to use functions not available within Tap Forms scripting. Could you provide a simple example or point me to one so I can learn?

    This is a really powerful feature that I’d like to be able to use.

    Thanks again,
    Mark

    August 29, 2019 at 11:07 PM #36610

    Sam Moffatt
    Participant

    The Utils.getJsonFromUrl() returns a JavaScript object directly. You don’t need to parse the JSON file, Tap Forms does that for you and creates a nested set of Javascript dictionaries and arrays.

    The Google Books API response is a little convoluted, what you want is a few levels deep. You might want to look into grabbing a JSON formatter for your favourite web browser to help see the layering in the responses with a tree format.

    Here’s something simple to get you further along:

    function Json_Request() {
    	let data = Utils.getJsonFromUrl('https://www.googleapis.com/books/v1/volumes?q=isbn:9780061143397');
    	console.log(data['kind']);
    	for (let i = 0; i < data['items'].length; i++)
    	{
    		let item = data['items'][i];
    		let volume = item['volumeInfo'];
    
    		console.log('Title: ' + volume['title']);
    		console.log('Print Type: ' + volume['printType']);
    		console.log('Authors: ' + volume['authors'].join(', '));
    	}
    }
    
    Json_Request();
    

    This is a form script I started, it’s intended to run from the editor and use the console to log out the data. You’d need to update it and change it to do what you want.

    EDIT: Ok, I think I understand where you might have gotten stuck.

    Javascript lets you use array syntax (e.g. the square brackets) to access dictionaries, arrays and also strings. To access a dictionary, you use a key lookup (e.g. data['items']); to access an array, you generally use numeric lookups (e.g. results[0]) and you can also access a string and it will give you the character at that offset.

    Quick example of the string output using a form script as a vehicle:

    var mystring = 'Test';
    for (var i = 0; i < mystring.length; i++)
    {
    	console.log(mystring[i]);
    }
    

    Will output the following in the console:

    T
    e
    s
    t

    Because it’s treating the string as an array of characters instead of giving you an error. I get caught by this from time to time and it sometimes takes me a moment to realise what I’ve done.

    If you find yourself hitting this then you’re probably iterating over a string instead of the array you were intending. Hopefully this helps.

    August 30, 2019 at 7:19 AM #36629

    Mark
    Participant

    Thanks very much for this information. I will work with this – I think you’ve cast a light on where I was confused.

    Much appreciated!

Viewing 7 reply threads

You must be logged in to reply to this topic.