Troubleshooting a misformatted JSON string that lacks proper double quotes in Java Script

{ DataError: { user_id: [ [Object] ] } }

I want to transform this string into JSON structure like below:

{ "DataError": { "user_id": [ [Object] ] } }

Is there a potential method to achieve this outcome from incorrectly formatted JSON string?

Answer №1

If you can confirm that the incorrectly structured string is secure and only contains poorly formatted JSON (meaning it won't run any additional javascript), one approach would be to use an eval function followed by JSON.stringify method.

JSON.stringify(eval('(' + customString + ')'));

Answer №2

I stumbled upon an amazing javascript library that saved the day for me. https://github.com/freethenation/durable-json-lint

It transformed my messy json string into a well-formed json string!

durableJsonLint = require('durable-json-lint');
console.log(durableJsonLint('{name:"value", \'array\':[call(), 0x11]}'))
// The above code will output the following to the console
{
   "json":'{"name":"value", "array":[null, 17]}',
   "errors":[{
         "column":1,
         "description":"Keys must be double quoted in Json. Did you mean \"name\"?",
         "lineNumber":1,
         "status":"correctable"
      },{
         "column":15,
         "description":"Json strings must use double quotes",
         "lineNumber":1,
         "status":"correctable"
      },{
         "column":24,
         "description":"You can not make function calls in Json. Do you think I am a fool?",
         "lineNumber":1,
         "status":"fail"
      },{
         "column":32,
         "description":"Invalid Json number",
         "lineNumber":1,
         "status":"correctable"
      }
   ]
}

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

combining all JavaScript script tags into a single tag

I'm currently working with wavesurfer.js and have the following setup: 'use strict' <!-- wavesurfer.js --> <script src="src/wavesurfer.js"></script> <script src="src/util.js"></script> <scrip ...

Leverage AJAX for real-time Django Model updates

Seeking insights on how to effortlessly update a Django model through AJAX without reloading the page or requiring user input for saving. Various tutorials address fetching data from Django models using AJAX, yet resources on updating models remain scarce. ...

React does not accept objects as valid children. If you want to render a group of children, make sure to use an array instead

I am in the process of developing a system for document verification using ReactJS and solidity smart contract. My goal is to showcase the outcome of the get().call() method from my smart contract on the frontend, either through a popup or simply as text d ...

Encountering a JSON parse error with a POST request to the SailsJS API

Trying to insert data into a MySQL database using the Sails.js API. This is the data being sent via an AJAX call: {"person":{"name":"Sahasrangshu Guha","address":"Dlf It Park Ii Block 1a, Plot No. Ii-F/1, Action A, Ericsson Kolkata","phoneNumber":"9830612 ...

Nodemailer is experiencing difficulties when used within the routes directory

Recently, I encountered an issue with my subscribe form. It functions perfectly when called from app.js where the express server is defined. However, when I move subscribe.js to the routes folder and connect both files, it fails to send emails. Upon pressi ...

Keep a vigilant eye on the peak utilization of memory within the Node.js process

I am in search of a way to effectively monitor the maximum memory usage in a Node.js process, regardless of whether there are memory leaks or not. The processes in question include both real applications and synthetic tests. My expectation is that it sho ...

Is it possible for two node scripts running on the same machine to interfere with each other's execution

In the scenario where a parent node file (such as an express server) spawns child nodes that execute computationally intense tasks, will these children cause blocking for the parent process? ...

Removing all HTML elements within the div container

My HTML code includes the following: <div class="row" id="conditional-one"> <div class="large-12 columns"> <h3><?php echo $arrayStage_rule_one_title?></h3> <p> <?php echo $arrayS ...

Removing other objects with Mongoose after an update

I'm facing an issue with my update query in mongoose. I can't figure out why other objects are getting deleted when I only intend to update one specific object. The code is functioning correctly in terms of updating, but it's causing all the ...

Mongoose - facing issues with $and operator, looking for a way to locate an array containing both items

I'm currently developing a chat-based application that involves private messaging between individuals. Essentially, a Chat model in my application consists of the following schema: let schema = new Schema({ members: [{ type: ObjectId, ref: models.u ...

In Python, extracting a particular value from a JSON string

How do I extract a specific value from a JSON response using Python? Below is the JSON string that contains the ID value I need to extract: "window.__store__ ="{ "listingReducer":{ "selectedListing":{ "id&q ...

Objects may unexpectedly be sorted when using JavaScript or Node.js

When I execute the following code using node app.js 'use strict'; var data = {"456":"First","789":"Second","123":"Third"}; console.log(data); I am receiving the following output: { '123': 'Third', '456': 'F ...

Using Javascript code within functions.php

I am facing an issue with the code below; function add_js_functions(){ $gpls_woo_rfq_cart = gpls_woo_rfq_get_item(gpls_woo_rfq_cart_tran_key() . '_' . 'gpls_woo_rfq_cart'); if(is_array($gpls_woo_rfq_cart)){ $count = count($gpls_woo_r ...

Deciphering enigmatic JSON data

public abstract class A { public string Foo{ get; set; } } public class B : A { public string Marco{ get; set; } } public class C : A { public string Las{ get; set; } } public class D : A { public string Las{ get; set; } } public class ...

Can you provide me with instructions on how to interpret JSON data obtained from a REST API?

I'm currently facing a challenge with reading JSON data obtained from a REST API. For fetching the JSON, I am utilizing Open::URI. The request setup is as follows: require "open-uri" require "json" content = open("http://foo.bar/test.json").read res ...

What is the process for storing data attributes in a database?

I am in the process of developing a website and I have a requirement to store certain variables from HTML in a database. The main goal is to save the paragraphs of text that users select and display them when the user revisits the page (similar to medium.c ...

Error: Unexpected character "<" found in JSON data at position 3 while using Ionic 2

I encountered an error in Ionic 2 that says "SyntaxError: Unexpected token < in JSON at position 3". The JSON format is correctly structured using spring boot. Here is the spring boot code: Your assistance is highly appreciated. @RequestMapping(value="/ ...

The editor is locked and choices are displayed in a vertical orientation

I'm currently experimenting with using draft js in my project to create a wysiwyg editor. However, I've encountered an issue where the editor appears vertically instead of horizontally when I load the component. Any idea why this might be happen ...

Navigating through Sails.Js: A Guide to Implementing Pagination

Looking to implement paginated tables in your application using sails.js, mongodb, and waterline-ORM? Wondering if there is a recommended method for pagination in sails.js? ...

The request's body in the PUT method is void

I seem to be having an issue with my PUT request. While all my other requests are functioning properly, the req.body appears to remain empty, causing this error message to occur: errmsg: "'$set' is empty. You must specify a field like so: ...