Decoding JSON by Parsing a Targeted Input String

Currently, Facebook API is delivering an array to me in this specific format:

to[0]=10100811338393761,to[1]=10100919262065481,...

I'm wondering if anyone has any ideas on how I can convert that into a JSON array using JavaScript?

Edit: To provide further clarity...

Facebook sends back an array of users who have received requests through the callback URL (view documentation here). It's returned in a URI format like so:

to%5B0%5D=10100811338393761....

After cleaning it up using decodeURIComponent, the code snippet I've shared is what remains.

My goal is to parse this into a JSON object to access the IDs, but whenever I attempt to use JSON.parse, I encounter errors ("unexpected [" or "unexpected =").

Answer №1

If the function returns PRECISELY as you described:

function convertToArray(input){
    var splitArray = input.split(/to\[[0-9]{1,}\]=(.*?),*?/g));
    var resultArray = [];
    if(!splitArray) return resultArray;
    for(var index = 0; index < splitArray.length; index++)
      if(splitArray[index] != '') resultArray.push(splitArray[index].replace(/,/g, ''));     
    return resultArray;
}


convertToArray('to[0]=10100811338393761,to[1]=10100919262065481')[0]
                === '10100811338393761'; // true

UPDATE: Rectified a regex bug. Improved formatting also.

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

Three.js - Expanding Your Field of Vision

How can I determine the width of the visible portion of a scene being rendered? For instance, if there is a mesh with a width of 100 units but is being displayed on the screen with a specific level of zoom, how do I calculate the actual width of the mesh ...

Guide on creating a readUInt16BE function in a Node.js environment

Looking to implement the readUint16BE function in node.js, here's how it is declared: buf.readUInt16BE(offset, [noAssert]) Documentation: http://nodejs.org/api/buffer.html#buffer_buf_readuint16be_offset_noassert This function reads an unsigned 1 ...

Implementing a sleek show/hide transition using slideToggle in jQuery

Trying to implement show/hide content with slideToggle and it's functioning, but the animation effect on the table is not smooth. Attempted two different codes, but none provided the desired animation effect: $('.more').slideToggle(' ...

When moving the cursor quickly, a vertical line does not appear upon hover

I am facing an issue with the vue-chartJs library. When I move the cursor fast, the vertical line on hover does not show up. However, when I move the cursor slowly, it works perfectly. Can anyone offer assistance in solving this problem? onHover: functi ...

Methods for generating arrays using jquery and modifying session data

How can I create arrays with jquery and modify the session? I am looking to store a list of songs with titles, mp3 URLs, and authors in $ _SESSION ["playlist"] Below is an example of my HTML code <a class="add-music" data-title="Title of the litt ...

Mapping Objects to JSON with RestKit

Currently dealing with a problem in RestKit where the serialization of my arrays is being wrapped with ( brackets instead of [ brackets. Is there any method to customize or set the delimiter/wrapper character for a specific data type? This leads to the f ...

Is there a way to use AngularJS foreach to extract targeted values by a specified key from a JSON object?

I have received a JSON object from an elastic search query as shown below: How can I extract the Agent and calls values from this JSON data? $scope.results = client .query(oQuery.query($scope.queryTerm || '*')) . ...

Placing brackets [ ] around each .json file within the folder

I have multiple individual .json files stored in a single folder. Here is an example of the file names: P50_00001.json P50_00002.json P50_00003.json P50_00004.json P50_00005.json.... This folder contains numerous such files. Upon opening any of these fil ...

AngularJS - navigating between sibling states using ui-router

I am currently incorporating bootstrap along with angularjs (and utilizing ui-router for routing). Within my navbar, each tab click should display a nested navbar within it. The nested navbar acts as a ui-view (is there a better way to approach this?). ...

JQuery table sorter is unable to effectively sort tables with date range strings

I am facing an issue with sorting a column in my table that contains text with varying dates. The text format is as follows: Requested Statement 7/1/2014 - 9/16/2014 When using tablesorter, the sorting does not work properly for this column. You can see ...

What is the best way to arrange the elements of an array based on a specified value?

Is there a way to develop a function that can organize an array of data based on the value of a specified field? Suppose the field consists of three numbers: 1, 2, 3. The idea is that upon providing a certain value to the function, it will rearrange the ta ...

Steps for converting a list with dictionaries to a JSON object while replacing single quotes with double quotes

As I work on my python script, I need to make an rpc call that requires a JSON array as arguments. rpc.command(JSONarray1, JSONarray2, 0, true) To generate the JSON arrays, I have created lists with dictionaries as elements. The dictionaries contain k ...

Are there any options to modify the default Bind timestamp format in the gin-gonic library?

Can someone help me with Go, specifically regarding gin-gonic and gorm? Let's consider a model like the one below // Classroom struct. type Classroom struct { gorm.Model Name string `json:"name"` Code string `jso ...

Exploring Unpredictable Motion with HTML5 and Javascript

I am currently diving into the world of HTML5 game development. I want to create a ball that moves randomly on the screen, rather than following a predictable left to right motion. Unfortunately, my current code only allows the ball to move in a set patter ...

Steps to make a sliding tab

I am currently faced with the challenge of coding the support tab on the right side of this page - . The tab currently slides out when clicked. I found the current tab slide-out effect here: My goal is to have both the main tab and the side tab appear wh ...

JavaScript Global Variables Keep Getting Reset

Here's the concept behind my project: I've created a simple game that utilizes a do/while function and a switch statement to determine the player's current room. When the player is in room 1, the switch selects room1 and executes the room1() ...

Retrieve the difference in time from the current moment using moment.js

I am trying to implement a functionality where I can track when my data was last updated. - After hitting the 'Update' button, it should display 'Update now' - If it has been n minutes since the update, it should show 'n mins ago& ...

Is it possible to extend the String prototype with the forEach method as found in the Array prototype?

It is common knowledge that there is a .forEach() method for arrays in JavaScript, but unfortunately Strings do not have that method integrated. So, the question arises: is it problematic to use the following code snippet: String.prototype.forEach = Array ...

Iterate through the array and add each number to a separate array

I am currently facing an issue with the code snippet provided below. var array = [1, 3, 2] var newArray = [] getNewArray() { for (let i = 0; i < array.length; i++) { for (let x = 0; x < array[i]; x++) { this.newArray.pus ...

I need to retrieve the Instagram follower count for a specific user using JavaScript based on their user ID

I'm looking to design a form that allows users to input their Instagram ID and receive the exact number of followers without the "k" abbreviation. However, I am unsure how to achieve this. Any guidance on how to accomplish this would be greatly apprec ...