What is the best way to set a JSON string as a variable?

I am attempting to send form input data to a REST service. Currently, the format is as follows:

{
  "locationname":"test",
  "locationtype":"test",
  "address":"test"
}

However, the service is only accepting the following format:

 {
    "value": "{ locationname: test ,locationtype: test, address:test }",
 }

I have tried converting the string using the code snippet below:

const tests = JSON.parse(JSON.stringify(Form.value));

but I am unsure how to assign it to Value.

My desired result after submitting the form would be:

{
  "value":"{ locationname: test ,locationtype: test, address:test }",
}

Answer №1

Perhaps this solution aligns with your specific needs. Make sure to utilize the "modifiedJsonObject" when submitting your data.

const formData = JSON.parse('{"name":"John","age":30,"city":"New York"}');
    const formString = JSON
      .stringify(formData)
      .replace(/"/g, '');
    const modifiedData = { info: formString };
    const finalString = JSON.stringify(modifiedData);

    // finalString = '{"info":"{name:John,age:30,city:New York}"}'

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

The Rails JSON gem is unable to convert a nil value into a hash

After successfully deploying my Rails application on my BlueHost server, installing all necessary gems, and launching the application using Passenger, I encountered an error message when trying to access the app. I couldn't figure out why the error c ...

Setting up APIGateway for CORS with the CDK: A Step-by-Step Guide

My API relies on AWS ApiGateway with an underlying AWS Lambda function provisioned through the CDK. The default CORS settings for the API are as follows: const api = new apiGateway.RestApi(this, "comments-api", { defaultCorsPreflightOptions: { ...

Removing leading zeros from numeric strings in JSON data

I am facing an issue with my jQuery-based JavaScript code that is making an Ajax call to a PHP function. updatemarkers.xhr = $.post( ih.url("/AjaxSearch/map_markers/"), params).done( function(json) { <stuff> } The PHP function returns the follo ...

Angular 6: Utilizing async/await to access and manipulate specific variables within the application

Within my Angular 6 application, I am facing an issue with a variable named "permittedPefs" that is assigned a value after an asynchronous HTTP call. @Injectable() export class FeaturesLoadPermissionsService { permittedPefs = []; constructor() { ...

Understanding the Union Type in Typescript and Its Application in Angular Development

I came across this piece of code: interface Course { code: string; name: string; user: number | { id: number; name: string; }; } This indicates that a course object can contain either the user object or the user key. When fetching the cour ...

What causes *ngIf to display blank boxes and what is the solution to resolve this problem?

I am currently working on an HTML project where I need to display objects from an array using Angular. My goal is to only show the objects in the array that are not empty. While I have managed to hide the content of empty objects, the boxes holding this co ...

The frontend is not triggering the Patch API call

I am having trouble with my http.patch request not being called to the backend. This issue only occurs when I try calling it from the frontend. Oddly enough, when I tested it in Postman, everything worked perfectly. Testing the backend on its own shows t ...

Converting newline characters to valid JSON format in GO

I am facing a challenge converting strings to JSON due to the presence of special characters, such as newlines, which can disrupt the JSON format. While using encoding/json, I noticed that passing a string literal works fine as it automatically adds neces ...

Updating the DOM through Long Polling allows us to maintain existing attributes in jQuery Plugin

UPDATE: The functionality is all set; the pushes are working. The only issue I'm facing is that each push resets the #load_info div to empty. Is there a way to retain the original content inside the div, even though the content will be an updated vers ...

Stop automatic scrolling when the keyboard is visible in Angular

I have created a survey form where the user's name appears on top in mobile view. However, I am facing an issue with auto scroll when the keyboard pops up. I want to disable this feature to improve the user experience. <input (click)="onFocusI ...

I am having trouble retrieving any data when using jQuery to post JSON data

I am struggling with some HTML and JavaScript code. Here is what I have: <html> <head> </head> <body> <script src="view/backoffice/assets/plugins/jquery/jquery-1.11.1.min.js" type="text/javascript"></sc ...

What is the proper sequence for binding jQuery to elements?

I'm currently facing a challenge with using jQuery to link a function to an element. Essentially, I'm loading a series of divs via JSON, with each item in the JSON having an "action" assigned to it that determines which function should be trigger ...

Having difficulty integrating requireJS and Node's Require in a single TypeScript project

I currently have a TypeScript project that is intended to work with both Node and the browser. In some scripts, I'm utilizing Node's require(), while in others requireJS's require(). The structure of my project directory is as follows: myPr ...

"Observables in RxJs: Climbing the Stairs of

Previously, I utilized Promise with async/await syntax in my Typescript code like this: const fooData = await AsyncFooData(); const barData = await AsyncBarData(); ... perform actions using fooData and barData However, when using RxJs Observable<T> ...

Adding a request parameter to an ajax Request for a jQuery dataTable that has been initialized from JSON and mapped by Spring can be achieved by including

I have a jQuery dataTable that is initialized from jSON and mapped by a Spring framework. Currently, I am not passing any parameters and retrieving all information. However, I now need to pass a single String to the Java method, most likely through a requ ...

Navigating through a collection of generic dictionaries in C#

I'm dealing with a json object that has been converted into a list of Dictionaries. The structure of the json is as follows: { "DataList": {"Non Fuel": { "sn":"/DataXmlProduct/Customers/DataXml/Customer/DueDate", "ItemCode":"/DataXmlProduct/Cus ...

Exploring NextJS with Typescript to utilize the getStaticProps method

I'm currently enrolled in a NextJS course and I am interested in using Typescript. While browsing through a GitHub discussion forum, I came across an issue that I don't quite understand. The first function provided below seems to be throwing an e ...

Angular routing unit testing: Breaking down routing testing into individual route testing sequences

Currently, I am in the process of testing the routing functionality of my Angular application: Below is the file where I have declared the routes for my app: import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@ ...

Mobile application for Android that utilizes JSON technology

I am currently working on developing an Android app and have encountered a problem. I have an API and need to write its data to a file. This is the code snippet for writing: string url2 = "http://new.murakami.ua/?mkapi=getProducts"; JsonValue j ...

Converting JSON data into a jQuery-powered spreadsheet

Recently, I completed a module on data visualization, where I learned how to transform Google Spreadsheets into JSON using jQuery. In my spreadsheet, there are two simple columns: date and status (collected data about myself for practice in visualizing it ...