I'm looking for a way to convert my typescript class with dictionary to a JSON object without the brackets.
this is my class
export class MediaTagRequest {
tags: Map<string, string>;
constructor(tags: Map<string, string>) {
this.tags = tags;
}
}
My instantiation
let tags = new Map<string, string>();
tags.set("city", "Karachi");
let mediatagRequest = new MediaTagRequest(tags);
const headers = { 'content-type': 'application/json'}
const body = JSON.stringify(Object.keys(mediatagRequest.tags.entries()));
My current output:
[["city","Karachi"]]
My desired output:
{
"tags": {
"city": "Karachi"
}
}
Can someone help me please, thank you.
You can use any of this to create object
and then create response body using it
Option 1
let jsonObject = {};
tags.forEach((value, key) => {
jsonObject[key] = value;
});
Option 2
let jsonObject = {};
for (let entry of tags.entries()) {
jsonObject[entry[0]] = entry[1];
}
Option 3
let jsonObject = {};
for (let key of tags.keys()) {
jsonObject[key] = value;
}
creating response body
const body = JSON.stringify({
tags: jsonObject
});