Exploring JavaScript capabilities with Google - managing and updating object names with numbers

After importing JSON data into Google Scripts, I am able to access various objects using the code snippet below:

var doc = Utilities.jsonParse(txt);

For most objects, I can easily retrieve specific properties like this...

var date = doc.data1.dateTime;
var playerName = doc.data1.playerName;
var playerId = doc.data1.playerID;
var teamNumber = doc.data2.personal.team;

However, when dealing with objects named with numbers, such as...

doc.data2.personal.team.87397394.otherdata
doc.data2.personal.team.87397395.otherdata
doc.data2.personal.team.87397396.otherdata
doc.data2.personal.team.87397397.otherdata

... I encounter an error message "Missing ; before statement" while trying to read the data with...

var teamId = doc.data2.personal.team.87397394;

I attempted accessing these objects using brackets, but received an "undefined" value in the log...

var teamId = doc.data2.personal.team[87397394];

I also tried converting the number to a string, but had no success...

var teamId = doc.data2.personal.team[+'6803761'];

Although I can read object names as strings using "For In", I'm struggling to access the objects themselves. Any help would be appreciated! Thank you, Brian.

UPDATE

Following suggestions, I attempted to store object names in a variable and use it within brackets. However, the test variable still remains "undefined"...

for(var propertyName in doc.data2.personal.team) {
    // propertyName refers to the object name
    // Value can be accessed like this: myObject[propertyName]
    Logger.log (propertyNames);
    var test = doc.data2.personal.team[propertyName];
}

The log does display object names successfully...
87397394
87397395
87397396
87397397

This issue might be related to a bug in Google's implementation. You can verify it by using the following example. The value of test will remain undefined...

function myFunction1() {
  var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
  var doc = Utilities.jsonParse(txt);
  for(var propertyName in doc.datablock_battle_result.vehicles) {
      Logger.log(propertyName);
      var test = doc.datablock_battle_result.vehicles[propertyName];
   }
}

Answer №1

It appears that the issue lies within the Utitlies.jsonParse function. Check out this working example

var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
var doc = JSON.parse(txt);
for(var propertyName in doc.datablock_battle_result.vehicles) {
    var vehicle = doc.datablock_battle_result.vehicles[propertyName];
    Logger.log('Vehicle id is ' + propertyName);
    Logger.log('Vehicle value is  ' + JSON.stringify(vehicle));
    break;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting JSON information into a mailto hyperlink

Can anyone help me figure out how to encode a mailto link properly with JSON data in the query parameters, ensuring that it works even if the JSON data contains spaces? Let's consider this basic example: var data = { "Test": "Property with spaces" ...

Automatically transforming SQL table data to JSON format

Is there an API available to extract my data from a SQL server database in JSON format? I only need it for demonstration purposes. I came across something similar like this, but I'm not sure how it works. I have Apache installed. Could someone provi ...

Leveraging the Google Geocode API through HTTP GET requests

I am facing a challenge in my HTML file where I have a map and I am using HTTP get with jQuery to retrieve a device's location. However, I am struggling to plot this location on the map. I need to parse the location information and display it accurate ...

Exploring the Power of Namespaces in ECMAScript 6 Classes

My goal is to create a class within the namespace TEST using ECMAScript 6. Previously, I achieved this in "old" JavaScript with: var TEST=TEST || {}; TEST.Test1 = function() { } Now, I am attempting the following approach: var TEST=TEST || {}; class TES ...

What is preventing me from accessing React state within Tracker.Autorun?

I'm feeling lost on this one. I have implemented a Tracker.autorun function to monitor when my Mongo subscription is ready for querying (following the advice given in this previous Meteor subscribe callback). It seems to be working fine as it triggers ...

Using JavaScript to Filter Through Numerous Values Based on Different Attributes

My goal is to efficiently filter multiple attributes with multiple values 'arr' represents a list of all products, and 'f_...' indicates the attribute such as color or type. 'namet' denotes the selected attribute by the user ...

Issue importing color palettes from JSON into Tailwind CSS

While working on my Vue.js project with Tailwind CSS, I ran into an issue. It seems that Tailwind is not picking up the custom color classes specified in my JSON configuration for 'primary', 'secondary', and 'gray'. Instead of ...

Send backspace key press event to the applet and prevent it from continuing

I am struggling with a Java applet on my webpage that allows users to edit text. Whenever a user tries to modify the text by pressing the backspace button, the browser mistakenly forwards to the previous page. I attempted to filter out the backspace key pr ...

How can I incorporate multiple graphs into my AmCharts display?

I am new to using amcharts and have successfully implemented a code snippet to generate two graphs in a chart. The charts are loaded from an external data source specified in the code. var chart = AmCharts.makeChart("chartdiv", { "type": "serial", "d ...

Tips for modifying the color or personalizing the header arrow for the expandableRow within Mui Datatable

My MUI data table has the expandable rows option, along with an ExpandAll button in the header row. The arrow displayed in the title row is confusing for users as it's similar to the arrows in all other rows. I want to change the color of the header a ...

Troubleshooting: jQuery ajax form is not functioning as expected

After attempting various methods to create a basic jQuery Ajax form, I am puzzled as to why it is not submitting or displaying any notifications. Below is the code I have been working with: Javascript ... <script type="text/javascript" src="assets/js ...

Encountering JSON Error When Working with Glassfish 4 REST XML

Currently, my web service on Glassfish 4.1 functions correctly when dealing with XML data, but encounters issues when handling JSON data. This is the structure of the entity class: @XmlRootElement public class Person implements Serializable { /** ...

Re-calculating with Jquery when input is updated

I am looking for guidance on how to make the calculator recalculate based on a change in the #calcTotal input field. I have successfully calculated all fields and managed to update the #cwac field when one of the values before it changes. Here is the HTML ...

The functionality of useMemo is compromised when changes are made to sessionStorage

I'm facing an issue with my app where the header contains an icon that should only be shown when the user is logged in. I store the login status in sessionStorage, but the component doesn't re-render when it changes. I attempted to use useEffect ...

What causes my backend to crash when incorrect credentials are entered?

Whenever I input incorrect credentials on my login page, it causes the backend to crash. I want to avoid this situation and prevent my backend from crashing. Can somebody please assist me in identifying and rectifying the mistake? I am using MongoDb as my ...

Utilize .GLB and Blender file formats within a Gridsome project developed with Vue.js

I am working on a project using Three.js in Gridsome, but I am facing difficulties importing .glb files (3D models) with GLTFLoader. While Gridsome recognizes img (png/jpg) or .js files stored in src/assets, it does not seem to support glb files. This is ...

Is the for loop in Node.js completed when making a MySQL call?

A certain function passes an array named res that contains user data in the following format: [ RowDataPacket { UserID: 26 }, RowDataPacker { UserID: 4 } ] The goal is to create a function that fetches each user's username based on their ID and stor ...

What adjustments can I make to my smooth-scrolling JavaScript code in order to exclude my Bootstrap Carousel from the scrolling

On my website, I have incorporated JavaScript to create a smooth-scrolling effect when a user clicks on a hyperlink. This feature is essential as the landing page includes a chevron-down icon that prompts the user to navigate to the next section of the pag ...

Attach a tab-icon to a picture

I am looking to add a tab to the bottom border of an image within a gallery. This tab needs to be right up against the image border with no space in between. When clicked, this tab should activate a small JavaScript function that will display the content ...

Encountering an unidentified entity within a ReactJS application

Within a container, I've included a search bar inside a form element with two inputs - from and to, along with a submit button. Upon submitting the form, I create an OBJECT named query which looks like this: const query = { from : this.s ...