Integrate guidelines into JSON stringify

I am working with a class called OrderParameterInfo:

class OrderParameterInfo {
Name: string;
Value: string;

My goal is to create a JSON string. When I use

JSON.stringify(OrderParameterInfo("foo_name", "foo_value"))
, it generates
{"Name":"foo_name","Value":"foo_value"}
. However, I would like the output to resemble a python dictionary, like this:
{"foo_name":"foo_value"}
.

Any suggestions on how I can achieve this?

Answer №1

Of course, it is possible to generate dynamic object keys in JavaScript. Take a look at this example:

const dynamicKey = 'dynamic_key';
const dynamicValue = 'dynamic_value';
const newObj = { [dynamicKey]: dynamicValue }; // { dynamic_key: 'dynamic_value' }

If you want to streamline the process and create a reusable function for this, you can do so with the following code:

const createDynamicObject = (key, value) => ({ [key]: value });

JSON.stringify(createDynamicObject('bar_name', 'bar_value'));

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

Struggling to retrieve accurate information from database (MSSQL) using PHP and JSON

There seems to be a small hiccup in my project. The query result is blank whenever the first name or last name contains the letter 'ñ'. Here's the code snippet: config.php: <?php ini_set('mssql.charset', 'UTF-8&apos ...

What is the best way to link a list of strings to an 8-character key?

On my WPF form, I have a ComboBox and a TextBox. The ComboBox contains options related to email addresses that the user will enter into the TextBox. I want each option in the ComboBox to be associated with an 8-character key, which will then be serialized ...

Conceal the PayPal Button

Currently, I'm facing a challenge where I need to dynamically show or hide a PayPal button based on the status of my switch. The issue is that once the PayPal button is displayed, it remains visible even if the switch is toggled back to credit card pa ...

Guide on accessing JSON field through GSON library with @SerializedName annotation

I am encountering an issue with parsing the JSON object provided below: "paymentCurrency": "eur", "paymentOptions": [ { "paymentOptionId": "1CeGuJt2nkxmaMVf", ...

Streaming JSON objects through a WCF REST service

Is there a way to create a Windows service where a REST API can dump JSON objects into a stream with the content-type of application/stream+json? Here's an example response (not an array of objects): { id: 1, name: "name 1" } { id: 2, name: ...

Generating Angular2 CLI components with Angular-Meteor integration

Exploring Angular2 CLI and Meteor has been an interesting journey for me. One thing I've noticed is that when I create a component using Angular2 CLI, integrating it into another module is as simple as including it in the declarations array of that mo ...

Adjusting a JSON query

Is there a way to update a Json using Excel VBA? I need the ability to modify the postcode dynamically with VBA or link this section to a cell in the workbook let Source = Json.Document(Web.Contents("https://api.propertydata.co.uk/prices?key=HEZEHOR0 ...

The error message received was: "The type 'List<dynamic>' is not a matching type of 'Map<dynamic, dynamic>' when performing a type cast."

Hey there, I'm just diving into the world of Flutter and ran into an issue while trying to access a folder name on my device. I used storage_Path to retrieve it, but encountered this error along the way. My app is set up to fetch the folder name from ...

What could be causing the Properties Array to come back as undefined?

When attempting to add an item to an array stored in the properties, I am encountering an error: "Cannot read properties of undefined (reading 'value')." Within the props, the following interfaces are defined: ILinkItemProps.ts export interface ...

The functionality of provideRouter and RouterConfig cannot be located in the updated @angular/router version 3.0.0-alpha.3^

Currently in the process of migrating an angular2 app to RC2 and experimenting with the router's version 3 alpha. Followed the setup provided by the angular docs for routing as demonstrated in the plunker. However, encountering the following errors: ...

Protecting your web applications: JSON wrapped in an Object

As cited in the Owasp CheatSheet Series, under the section 'Protect against JSON Hijacking for Older Browsers', it is recommended to always structure JSON with an object on the outside to prevent exploitation. According to the same source, JSON i ...

Performing an HTTP request response in JavaScript

I am trying to make an HTTP request that returns the data in JSON format using 'JSON.stringify(data)'. var xhr = new XMLHttpRequest(); xhr.open("GET", "/api/hello", true); xhr.send(); xhr.onreadystatechange = function () { console.log(xhr.r ...

How to convert an array of keys and an array of values into an array of objects using JavaScript

My question is similar to the one found here: Merging keys array and values array into an object using JavaScript However, I am unable to find a solution for my specific scenario. If I have these two arrays: const keys = ['x', 'y', &ap ...

Loading JSON will display just a single value

Issue arises when JSON reads a file in the load method. I will explain further in the comments. Please note that some code has been omitted for clarity. private static final String ID = "moti_data"; public static void save(Context context) throws IOExce ...

What benefits do Definitely Typed TypeScript files offer for Knockout and jQuery?

I'm not sure if this is a silly question, but I was wondering if it's necessary to use definitely typed (.d.ts) versions of external libraries when working with Typescript. Currently, my code base uses jQuery and Knockout in the traditional manne ...

What is the best way to deserialize a JavaScript date with Jackson library?

I receive a date string from ExtJS in the format: "2011-04-08T09:00:00" When attempting to deserialize this date, it automatically adjusts the timezone to Indian Standard Time (adding +5:30 to the time). Here is my current method for deserializing the ...

The Interface in TypeScript will not function properly when used on a variable (Object) that has been declared with the value returned from a function

I am currently in the process of developing an application using Ionic v3. Strangely, I am encountering issues with my interface when trying to assign a variable value returned by a function. Here is an example that works without any problems: export int ...

What factors contribute to TypeScript having varying generic function inference behaviors between arrow functions and regular functions?

Consider the TypeScript example below: function test<T = unknown>(options: { a: (c: T) => void, b: () => T }) {} test({ a: (c) => { c }, // c is number b: () => 123 }) test({ b: () => 123, a: (c) => { retur ...

Mastering Light and Camera Selection in Three.js

Question, In the editor found at this link, you can click on a light or camera to select it. I am familiar with using raycaster.intersectObjects(objects) to select meshes, but how can I achieve the same result for lights and cameras which do not have mesh ...

ng-repeat to display items based on dropdown choice or user's search input

Utilizing $http to retrieve JSON data for display in a table. I have successfully implemented a search functionality where users can search the JSON data from an input field. Additionally, I now want to include a feature that allows users to filter the JSO ...