Device sending incorrect JSON format

A JSON response from a device is as follows:

{
  "Order": ""3"",
  "ID":    ""SHEET"",
  "Name":  ""Time Sheet"",
  "ConID": ""!s?d2SzxcSDW1cf$*@4812sC#""
}

The property values in the JSON are enclosed in double quotes, causing them to be incorrect.

Is there a method to correctly parse or reformat this response into a valid object?

I attempted to replace "" with "\" but I am uncertain if it worked accurately.

Answer №1

To easily switch out all instances of "" with ", simply utilize String.replace alongside a regex for global substitution. Then, proceed to employ JSON.parse:

let jsonData = '{"Order":""3"","ID":""SHEET"","Name":""Цагийн бүртгэл"","ConID":""!s?d2SzxcSDW1cf$*@4812sC#""}';
jsonData = jsonData.replace(/""/g, '"');
const dataObj = JSON.parse(jsonData);
console.log(dataObj)

Answer №2

It is highly likely that the device responsible for the invalid JSON response utilizes a software that indiscriminately inserts values (regardless of their content) into the response's properties, always enclosing them in double quotes.

If you have access to the code generating the JSON on the device, try locating the section of code that constructs the final JSON output. There are two potential solutions:

  • Eliminate inner double quotes from original strings (not an ideal solution)
  • Remove outer double quotes instead (this will ensure numbers remain as numbers and are not converted into strings)

If you do not have access to the source code of the device producing the JSON, consider utilizing @Nick's solution to eliminate the redundant double quotes.

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

Why isn't the AngularJS injected view resizing to fit the container?

As a novice delving into a project in the MEAN stack, I'm encountering inconsistent HTML previews. When I view it independently versus running it from the project, the display varies. Here's the intended appearance (in standalone preview): imgu ...

Is it necessary to sanitize input fields manually in Angular 6?

Is it necessary for me to manually sanitize all user inputs, or does Angular handle this process automatically? In my login form, the data is sent to the server upon submission. Do I need to explicitly sanitize the data, or does Angular take care of sanit ...

Tips for storing a list of files within another list as a JSON file in Python

After successfully parsing data from a website using BeautifulSoup in Python, I have managed to extract the desired information. Now, my goal is to save this data in a JSON file. However, when I attempt to do so with the existing code, the data gets saved ...

Is there a way to configure my datepicker so that it displays only dates that are later than a specified date

At the heart of my inquiry lies a dilemma: I have two date pickers, one for leave_start and the other for leave_end. If an individual selects "YES" for a future text_field, I aim to ensure that the date pickers only display dates after the person's an ...

A guide on dynamically sending data to a PHP script when an input is changed and displaying the results in a

I am trying to implement a feature where the data inputted into a text field is sent to posttothis.php and then the result is displayed in a content div. However, I am encountering difficulties in making it work. testscript.html <html> <head> ...

transferring JSON information to a template in a NodeJs application

Currently, I am performing some filtering on my JSON data and storing the result in a variable called driver. The driver variable contains JSON data that I want to pass unchanged to the view. My main query is: How can I effectively send the data stored i ...

Using NodeJS to extract information from Opendata JSON files

I'm currently working on a project that involves fetching JSON data from an Open Dataset using NodeJS and passing it to angular. The challenge I'm facing is that I'm receiving a JSON file instead of a JSON object, which makes it difficult to ...

Tips on enhancing an array by separating values with vertical bars instead of commas

I am trying to store checked values in an array and separate them with vertical bars instead of commas. Is there a way to achieve this using the jQuery map .get() function? Any suggestions or links you can provide would be greatly appreciated. Thank you in ...

The React application is experiencing difficulties in receiving the response data (JSON) from the Express server, despite the fact that

When making POST or GET requests to our Express server, served through PM2 on EC2, Postman receives the complete response with JSON data. However, our front end React app (both locally and deployed via CF) only gets the response status code and message. Th ...

Working with structured data in Golang by accessing a variable within a struct that contains an array of other

func GetprofilesApi(c *gin.Context) { var p Profile profiles, err, count := p.GetProfiles() if err != nil { log.Fatalln(err) } c.JSON(http.StatusOK, gin.H{ "Number of Results": count, "profiles": profiles, }) } //Getprofiles() function func ...

What is the method for displaying a decoded JSON array?

I am currently studying JSON. getDatas.php script: $query = "SELECT domain FROM access WHERE userid = '".$userid."'"; $result = mysql_query($query) or die(mysql_error()); $res = array(); while($row = mysql_fetch_array($result)){ $query2 = ...

Seeking specific parameter in a JSON array using JavaScript: A guide

Currently, I am working on a project that involves retrieving Facebook news feed data using the graph API. Upon receiving the JSON object, I display it on the page. The "Likes" section is presented as an array of JSON objects like this: data.likes: { ...

Obtain asynchronous result from updating state in React

I am trying to achieve the following: myFunction = () => { this.setState( state => { const originalBar = state.bar; return { foo: "bar" }; }, () => ({ originalBar, newBar: state.foo }) //return this object ...

The option list in AngularJS is cleared when an option is selected

In my current project, I am developing a django-tastypie api application with angularjs as the JavaScript framework. The main part of this application involves managing curriculum objects, each containing a list of grade objects and each grade object furth ...

Secure verification is a critical element within the core component of React JS

Creating a user-based application using Meteor (v1.3) requires strong authentication and authorization mechanisms. I found an insightful example by the author of Flow Router that delves into setting up authentication and authorization using Flow Router. h ...

What are the best practices for incorporating CSRF tokens into Java applications to ensure security?

The Challenge: I encountered an issue with preventing CSRF attacks in my Java web application. To tackle this problem, I attempted to utilize the X-CSRF-Token implementation. Every time a request was sent, it looked something like this: POST /sessions HTT ...

Tips for obtaining latency measurements from the DailyMotion player during a live video stream

Is there a way to determine the latency of DailyMotion live video for synchronization with events, such as displaying speaker names alongside the video? I have been utilizing the DailyMotion player API in JavaScript but haven't found any information r ...

Extracting URLs from dynamic JavaScript page using BeautifulSoup and Selenium

Seeking to extract all URLs from a Git repository where any email addresses appear. Utilizing The script: from bs4 import BeautifulSoup from selenium import webdriver url = 'https://grep.app/search?current=100&q=%40gmail.com' chrome = " ...

Reversing data in Vue3

I've been struggling to create a custom dropdown for my application. I need to implement a function that adds a class called is-active to a div element. So, what I have done is created a simple div with an onclick function as shown below: <div :cla ...

Navigational elements, drawers, and flexible designs in Material-UI

I'm working on implementing a rechart in a component, but I've encountered an issue related to a flex tag. This is causing some problems as I don't have enough knowledge about CSS to find a workaround. In my nav style, I have display: flex, ...