Verifying the similarity of usernames using a JSON array

My attempts to create a function that verifies if app.account_username matches a username in a json array are just leading me in circles.

This is how the json array is structured

{
 "data": {
"id": 32607158,
"with_account": "user1",
"with_account_id": 8483786,
"last_message_preview": "XD",
"message_count": 289,
"messages": [{
  "id": 153359194,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711004
}, {
  "id": 153359270,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711023
}, {
  "id": 153359330,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711044
}, {
  "id": 153359334,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711048
}, {
  "id": 153359386,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711057
}, {
  "id": 153359430,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711065
}, {
  "id": 153359474,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711078
}, {
  "id": 153359522,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711090
}, {
  "id": 153359638,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711106
}],
"done": false,
"page": 2,
"datetime": 1439711387
},
"success": true,
"status": 200
}

I'm attempting to compare "from" with "app.account_username", but I could use some guidance on how to accomplish this. Does anyone have any ideas?

Answer №1

To convert this json string into an object and cycle through its array to validate each element against app.account_username

let jsonData = '{ "data": { "id": 32607158, "with_account": "user1", "with_account_id": 8483786, "last_message_preview": "XD", "message_count": 289, "messages": [{ "id": 153359194, "from": "user1", "account_id": 20094939, "sender_id": 8483786, "body": "*hugs*", "conversation_id": 32607158, "datetime": 1439711004 }, { "id": 153359270, "from": "user2", "account_id": 20094939, "sender_id": 20094939, "body": "*hugs*", "conversation_id": 32607158, "datetime": 1439711023 }, { "id": 153359330, "from": "user1", "account_id": 20094939, "sender_id": 8483786, "body": "*hugs*", "conversation_id": 32607158, "datetime": 1439711044 }, { "id": 153359334, "from": "user2", "account_id": 20094939, "sender_id": 20094939, "body": "*hugs*", "conversation_id": 32607158, "datetime": 1439711048 }, { "id": 153359386, "from": "user1", "account_id": 20094939, "sender_id": 8483786, "body": "*hugs*", "conversation_id": 32607158, "datetime": 1439711057 }, { "id": 153359430, "from": "user2", "account_id": 20094939, "sender_id": 20094939, "body": "*hugs*", "conversation_id": 32607158, "datetime": 1439711065 }, { "id": 153359474, "from": "user1", "account_id": 20094939, "sender_id": 8483786,...
    
let objData = JSON.parse( jsonData );
        
for (let index = 0; index < objData.data.messages.length; index++) 
{
    if(objData.data.messages[index].from == app.account_username)
        {
            doSomething();
            break;
        }
}

Answer №2

Take a look at this interesting jsfiddle here

It showcases the display of user1 and user2 in a unique way.

var sample = "your json information"
    for(var j=0;j<5;j++)
alert(JSON.stringify(sample.data.messages[j].from));

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

Which is causing the block: the event loop or the CPU?

example: exports.products = (req, res) => { let el = 1; for (let i = 0; i < 100000000000000; i++) { el += i; } console.log(el); ... ... ... res.redirect('/'); }; If I include a loop like this in my code, which resour ...

Guide to utilizing AJAX to retrieve a value from a controller and display it in a popup

I need assistance with retrieving data from a controller and displaying it in an HTML pop-up when a button is clicked. Currently, the pop-up shows up when the button is clicked, but the data is not being loaded. I would like the values to be loaded and d ...

A simple method to obtain the ID of the element that has been clicked and save it in a variable to be utilized in a function responsible for

Seeking assistance in optimizing the code below by removing the specific #static id and allowing for dynamic IDs such as #dynamic within the one click function. This would eliminate the need to repeatedly copy and paste the same function with different ID ...

Converting a JSON object to an array using Node.js

Photos: - id: Photo1 Type: Color Shade: Grey - id: Photo2 Type: Color Shade: Red Presented here is the conversion of the YAML file above into a JSON Object, with an attempt to locate a photo based on its unique ...

Utilize JSON parsing with AngularJS

My current code processes json-formatted text within the javascript code, but I would like to read it from a json file instead. How can I modify my code to achieve this? Specifically, how can I assign the parsed data to the variable $scope.Items? app.co ...

Gulp does not work well with Android APK compilation

Greetings, I am facing an issue while trying to compile my Android app using gulp with the command: gulp --prod -p android. The problem arises when comparing the file size of the generated APK between myself and a colleague. When my colleague compiles, the ...

Managing JSON Forms using jQuery on Google's App Engine

Having difficulty getting jQuery to function properly on GAE in python 2.7. The main issue is that jQuery is unable to retrieve the json data sent by the server. A basic comment form with no special features: <form action="/postcomment/" method="post" ...

Even in the absence of the element on the page, the VueJS instance is still being invoked

I am encountering an issue where all my VueJS instances are being executed, even if the element associated with them is not present on the page. Currently, I have defined a mixin as follows: var mixin = { methods: { listEvents(parameters) { ...

Looking to retrieve Json data in Golang utilizing URL parameters

Working on a Golang server that fetches data from a local JSON file. I've set up the necessary structures and can return all data from the JSON file, but how do I retrieve specific entries? Is there a way to query the data? I'd like users to inp ...

Using jQuery to retrieve the unique identifier of an element and checking if it matches a certain value before including the link

I'm currently using this code to retrieve values from a database and then add them to a form. The initial part appends the name of the Firewall and includes the unique ID in the element's id. if (value.type == 'Firewall') { $(' ...

Retrieve data from Firestore using React and log it outside of the asynchronous function

Seeking a way to retrieve data from userRef and use it to initiate another asynchronous call to another document (e.g. bikes) in order to display that bikes doc on the page Currently, the userDetailslog in the Home screen function prints {"_U": ...

Having difficulty executing a function inside a JavaScript file

Within my index.php file, the following lines of code are present: <!doctype html><html class=""><head><title>a title</title> <link href="_css/boilerplate.css" rel="stylesheet" type="text/css"> <script type="text/jav ...

I'm experiencing an issue with fullCalendar where the dayRender function is not functioning as expected

I have been using fullCalendar and I am looking to customize the color of specific days. I have successfully created an overlay that is displayed when a user clicks on a particular day. Everything works as expected with the overlay, but now I am encounte ...

What is the reason that filter_input does not work for retrieving form data when $_GET functions without any issues?

Currently, I am utilizing the get method along with a form to save the status of checkboxes into an array. I have made an effort to utilize a filter_input line to grab details from each checkbox one by one and store it in an array. Everything seems to wor ...

Once deployed, Google Cloud Function experiences delayed execution

I have implemented a Cloud Function that executes the following steps: retrieving data from an API formatting the retrieved data saving the data in Firestore Here is the code snippet for my implementation: exports.syncItems = functions.https.onRequest((r ...

Is it possible to implement a custom radio tab index without using JavaScript

Is it possible to apply the tabindex attribute on custom radio buttons, hide the actual input element, and use keyboard shortcuts like Tab, Arrow Up, and Arrow Down to change the value? Check out this example on StackBlitz ...

Output the elements of each PHP array on separate lines within a text file

Utilizing a while loop, I have been able to retrieve rows from a database and store them in an array. Now, my objective is to have the elements of the array written on separate lines in a text file during each iteration of the loop. I attempted using imp ...

Struggling to access content with Protractor Promise callbacks. What is the best approach for invoking methods on the returned item?

Upon hitting the debugger in the code snippet below, the value of 'thing/item' shows as empty (refer to the image). it('CheckAllLinks:', function () { browser.ignoreSynchronization = true; browser .findElements(by.tagNa ...

Guide on effectively sending a secondary parameter in an ajax request

Struggling to implement a year/make/model multi-select feature, I have managed to get the scripts working. However, what appears to be missing is the ability to pass both a year and make variable together in order to fetch the final model results. Below a ...

Is it possible to transfer an array from one file to another file without knowing the array's name?

In my file named test.php, I have an array structured like this: array( [0] => $x_axis_t1 = "1,2,3,4,5"; [1] => $y_axis_t1 = "1,2,4,3,2"; [2] => $x_axis_t2 = "1,22,73,49,5"; [3] => $y_axis_t2 = "1,9,8,2,1"; ) I now need to access or ...