Is there a way to efficiently parse and categorize erroneous JSON data in JavaScript?

Resolved my issue

var allKeys = ["key","en","ar"];
for(var i=0;i<allKeys.length;i++) {
  for(j=0;j<jsonText.Sheet1.length;j++) { 
   console.log(allKeys[i] + ' - ' + jsonText.Sheet1[j][allKeys[i]]);
  }
}

Live demonstration

Appreciation to @Aniket Sinha


How can I parse and organize incorrect JSON data?

The invalid JSON data provided;

{
  "Sheet1": [
    {
      "key": "title",
      "en": "title",
      "ar": "arabic_title", //comma at the end here
    },
    {
      "key": "content",
      "en": "content",
      "ar": "arabic_content",  //comma at the end here
    }
  ]
}

Desired output:

key - title

key - content

en - title

en - content

ar - arabic_title

ar - arabic_content

Answer №1

Understanding JSON.parse() Method

var data = JSON.parse('{"Sheet1":[{"key":"title","en":"title","ar":"arabic_title"},{"key":"content","en":"content","ar":"arabic_content"}]}');
var key = data.Sheet1[0].key;
key === "title";

UPDATE:

The JSON string provided contains errors in formatting. There should be no comma after "ar": "arabic_title" and "ar": "arabic_content".

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

Having trouble with bootstrap carousel malfunctioning on Firefox browser?

I'm experiencing an issue with the carousel slider on my website. It seems to only be working in Chrome and not in Mozilla Firefox. You can view the live site at: Below is the code I have used for the carousel: <header id="myCarousel" class="car ...

Step-by-step guide on generating an index through mongoose and elastic search in a node.js and express.js environment

I am looking to set up the index in elastic search using mongoose and express, but I have not been able to find any documentation on how to do it. I attempted to use mongoosastic, but it did not meet my needs. Is there anyone who can assist me with this? ...

Optimal techniques for leveraging CSS within Mui and Reactjs

Just starting out with mui, I'm currently working on styling CSS for mui components like so <Typography variant="h5" sx={{ fontWeight: "bold", color: "#1a759f", display: "flex", ...

Using Webdriver to dynamically enable or disable JavaScript popups in Firefox profiles

I am currently working on a test case that involves closing a JavaScript popup. The code functions correctly in a Windows environment, but when I try to deploy it on a CentOS based server, I encounter the following error: Element is not clickable at point ...

Verifying JSON Schema in Java: Ensuring Data Integrity

Back in 2019-09, I developed a JSON Schema (). Does anyone know how to validate it using Java? I need assistance creating a method that will throw an Exception if the validation fails: void validate(Path pathToSchema) throws Exception { // Validate sch ...

Fetch Timeout Issue in React Native

I am currently using fetch to retrieve data from a server. Unfortunately, I am facing issues with setting a timeout for the fetch request in case the server does not respond. This is my global fetchData class: fetchGetResp(url, token) { return fetch( ...

Tips for updating status while parsing a hefty JSON file using GSON

Greetings to the Stackoverflow community, I am currently dealing with parsing a large JSON file from my raw resources. To avoid an out of memory exception, I transitioned from reading line by line to utilizing a Reader object in conjunction with Gson. So ...

Is it possible to have unique color tags in Material UI Autocomplete?

I'm currently diving into the world of material-ui and encountering some challenges that I could use help with. I am trying to incorporate multiple arrays into the autocomplete menu (e.g., options={top100Films, top100Shows}, but with the correct sy ...

req.body is not defined or contains no data

I am facing an issue with my controllers and routers. bookController.js is functioning perfectly, but when I try to use userControllers for registration and login logic, req.body always appears empty. I tried logging the form data using console.log, but it ...

Is your jQuery .on function failing to properly detect click events?

Seems like I'm missing something here. Why is the .on function not functioning as expected in this code snippet? <html> <head> </head> <body> <button type="button" class="close" data-test="test">TEST BUTTON< ...

What methods can be used to access web content with Java programming language?

Reading this foreign language content online has been quite challenging for me. Currently, I am working on an android app that requires reading JSON data from the web. Despite my attempts to figure it out on my own by Google searching, nothing seems to be ...

What methods does Angular use to display custom HTML tags in IE9/10 without causing any issues for the browser?

Exploring web components and utilizing customElements.define tends to cause issues in IE9/10. I've noticed that Angular is compatible with IE9/10, and upon inspecting the DOM tree, it seems like Angular successfully displays the custom element tags. ...

Utilizing a mutual RxJS subject for seamless two-way data binding in Angular 2

I have a unique service dedicated to managing app configurations class Configuration { get setting() { return dataStore.fetchSetting(); } set setting(value) { dataStore.saveSetting(value); } } This configuration is linked to components t ...

The dilemma between Nuxt Js global CSS and Local CSS

Currently, I am in the process of developing a Nuxt application utilizing an admin template. While I am well-versed in Vue.js, I am finding the aspect of loading assets within Nuxt.js to be a bit perplexing. I am in the process of converting the admin temp ...

Automated Form Submission: A Guide to Automatically Sending the Form After Captcha Verification

I'm looking to incorporate ReCaptcha into my website in order to display additional data. My goal is to have the form automatically submitted once the ReCaptcha has been completed, eliminating the need for an extra submit button. However, I've en ...

Are two JavaScript functions conflicting with each other?

Seeking assistance with integrating a Javascript/PHP/AJAX clock into my website to display various timezones (tutorial link: ) The clock implementation is functional, but it conflicts with an existing javascript stopwatch on the page, causing the clock no ...

I'm having trouble getting my button to work with addEventListener while using Ejs and Express. What could

Creating a Twitter-like platform where I can post tweets and have them display on the same page has been quite challenging. I've set up each post to have an Edit button initially hidden with 'display:none'. However, when I try to click on th ...

What is the best way to pass a dynamically generated component as the ng-content for another dynamically generated component?

Need help with creating a modal for our library using viewRef.createComponent. I want to generate Component A and embed Component B inside it, utilizing <ng-content> to fetch content from Component B. Component A serves as the modal template, while ...

Strategies for managing the #document element within an iframe

While testing the current portal, I encountered an issue where I couldn't create any xpath locators. After some investigation, I realized that the problem stemmed from an '#document' causing the path to be cut off and redirecting the "copy x ...

Is it possible to combine a string with a dynamic value in-line?

Looking to retrieve values from a JSON object dynamically? Here's an example: var valueOne = jsonObj.features[0].properties.POLYGON_NM.ToString(); If you want to collect multiple values without manually typing each option, you can use a syntax like ...