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

Understanding the process of parsing JSON data in an Android application

I have the following JSON to parse. Currently, I am parsing it using multiple if else loops. Is there a more efficient and standard way to parse this JSON format? Any help would be greatly appreciated. JSON: { "Info": { "DeviceInfo": { ...

Prevent IonContent from scrolling to the bottom or top when using Ionic framework

In my Ionic app, I have a long text page with 2 buttons that trigger the actions scrollToBottom and scrollToTop. Due to the length of the page, I have set the scroll duration to be 30 seconds. I am facing two issues here: How can I stop the scrolling ...

The route path consists of data that will be retrieved before entering the route

I am facing an issue with my router configuration. I have a dynamic Route that requires the last part of its path to be set after fetching data from a store call in beforeRouteEnter. beforeRouteEnter (to, from, next) { if(to.params.categoryId) { next(vm ...

Node.js has the ability to establish internal connections, however it cannot establish connections

I'm having an issue connecting to a JavaScript file on my local server. I'd like to input the external IP address and port in order for it to run externally. This functionality currently works when accessed locally. Below is the code from my serv ...

Requests exceeding 8 kilobytes

I am looking to measure the round trip time between a client and server. Users have the option to determine the size of the request/response message they want to send. On the client side, I am using the Ajax-Post method to transmit 100 messages every 100 m ...

Submitting a form using jquery

I am working on a project that involves using a jquery fancyzoom box. Within this box, there is a contact form that should send an email upon submission. However, I am encountering issues with calling the form submit function due to the fancyzoom feature. ...

Troubleshooting the sidebar pin-unpin problem using Jquery and CSS

I have created a single side panel that allows me to toggle between opening and closing the sidebar by hovering on it. I can also pin and unpin the sidebar by clicking on the pin image. Now, I want to open the sidebar onClick instead of onHover and remove ...

Examining the dimensions of a div element in AngularJS

As I delve deeper into understanding AngularJS and tackling the intricacies of how $watch operates, a specific scenario has caught my attention. I want to monitor and track changes in the dimensions of the div element with an ID of "area". My intention is ...

Using JavaScript to display a confirmation dialog box with the content retrieved from a text input field

Can a confirm dialog box be used to show the user-entered value from a form's text box? For instance, if the user enters 100.00, I want the dialog box to say something like, "Confirm Amount. Press OK if $100.00 is accurate." ...

Inserting HTML content into a DIV with the help of jQuery

Looking for a solution with my index.html file. I want to load other HTML files into a DIV by clicking on buttons. Here's an example of what I'm trying to achieve: I've searched through various examples online, but none seem to work for ...

What is the best way to utilize regex for replacing a string within Node.js environment?

I'm struggling with updating the string in my code. Essentially, I have a .php file and I want to modify some constants using the replace package. The output of my current code is: // Current Output define('DB_NAME', 'foo'); // ...

unexpected issue when trying to append a child element after creating its innerHTML

Upon executing this script, I expect to see the initially empty div with id="checkboxes" transformed into: <div id="checkboxes"> <label><input type="checkbox">option1</label> <label><input type="checkbox">opt ...

What benefits does JSON/XML offer compared to a simple variable string format?

What benefits come with using JSON/XML instead of a raw variable string format? In our iPhone application, we currently receive a response in the form of a raw string from the server after posting to a URL. What advantages could switching to JSON/XML prov ...

Transmitting occasional feedback from ASP.NET Web API function to jQuery AJAX

I am currently working on a project that requires sending intermittent status responses from a Web API method back to a jQuery AJAX call in order to display progress in the UI. The process involves the jQuery AJAX calling the Web API method with specific ...

The website code lacks a dynamically generated <div> element

When using jQuery to dynamically add content to a "div" element, the content is visible in the DOM but not in the view page source. For example: <div id="pdf"></div> $("#btn").click(function(){ $("#pdf").html("ffff"); }); How can I ensur ...

The issue with the autoresize feature in the tinymce plugin arises when trying to delete HTML img content using the backspace

When using the tinymce editor with the autoresize plugin enabled, I have noticed that it works correctly with text. However, there is an issue when inserting HTML content via execCommand. For example, if I insert the following code: <div> < ...

What is the best way to ensure that an iframe adjusts its height to fit the content within a tabbed container?

Is there a way to dynamically set the height of an iframe to match the content inside it when clicking on a tabbed plane? Initially, I used a fixed height of 1000px. How can this be achieved? <div class="container-fluid text-center"> <div ...

Encountered an issue parsing the JSON file, possibly due to a concealed value within the JSON content

After coming across this JSON file online: https://drive.google.com/file/d/1zh_fJJNWs9GaPnlLZ459twSubsYkzMi5/view?usp=share_link Initially, it seemed fine and passed various online json schema validators. However, upon attempting to parse it locally usin ...

Extracting and retrieving the value from the paramMap in Angular/JavaScript

How can we extract only the value from the router param map? Currently, the output is: authkey:af408c30-d212-4efe-933d-54606709fa32 I am interested in obtaining just the random "af408c30-d212-4efe-933d-54606709fa32" without the key "authke ...

When using NodeJS with expressJS, remember that req.body can only retrieve one variable at a

I'm having trouble with my login and signup pages. The login page is working correctly, but the signup page is only transferring the password for some reason. App.JS: var express = require("express"); var app = express(); var bodyParser = require("bo ...