This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
How to convert URL parameters to a JavaScript object? [duplicate]
(34 answers)
Closed 27 days ago.
I have this string:
"username=admin&password=systempass*&setCookie=true&id=E4-30-22-3B-00-E
and just whant convert it like:
{
"username: "admin",
"password": "systempass*",
"setCookie": true,
"id": "E4-30-22-3B-00-E"
}
so I tried whit this code:
let text = "username=admin&password=systempass*&setCookie=true&id=E4-30-22-3B-00-E";
const arr = text.split("&");
const json = JSON.stringify(arr);
console.log( json );
but don't work it because I'm getting ["username=admin","password=systempass*","setCookie=true","id=E4-30-22-3B-00-E"]
I don't know what to do for get correct json, what I have to add??
maybe...
let text = "username=admin&password=systempass*&setCookie=true&id=E4-30-22-3B-00-E";
let params = Object.fromEntries( new URLSearchParams(text) );
console.log( params )
Related
This question already has an answer here:
Dynamic object property names?
(1 answer)
Closed 8 months ago.
so i have this JSON file:
{"fjord-200-200.jpg":[]}
I read it using
let data = fs.readFileSync(cachedName);
let cachedInJSON: JSON = JSON.parse(data.toString());
so i have now JSON object, that i want to append to this new JSON object
let newData = {
fileNameFormatted: []
}
when i tried using
let newJson = {...cachedInJSON, ...newData}
Result was this:
{"fjord-200-200.jpg":[],"fileNameFormatted":[]}
Wanted result that i want to achieve is:
{"fjord-200-200.jpg":[],"fjord-300-300.jpg":[]}
fileNameFormatted is a variable holding fjord-300-300.jpg
let newData = {
[fileNameFormatted]: [] // Notice the brackets around the key
}
This question already has answers here:
Javascript nested objects from string
(5 answers)
Closed 1 year ago.
I have the following string:
let str = "modules.mas.mas-helper-provider.assets.locales";
and would like to convert it to a nested JavaScript object (JSON), something like this result:
{
"modules": {
"mas": {
"mas-helper-provider": {
"assets": {
"locales": ""
}
}
}
}
}
You can split the string to an array, then reduceRight to create an object by reading each key.
let str = "modules.mas.mas-helper-provider.assets.locales";
var newObject = str.split(".").reduceRight((obj, next) => ({
[next]: obj
}), "");
console.log(newObject);
This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 3 years ago.
I am working in react. I have a constant like
const priorityMap = {
"medium":"clean",
"high":"breaker",
"low":"promote"
}
If I do the following , I am getting result.
const map1 = priorityMap["medium"];
console.log("print checkgroup " + map1)
But I want to do the reverse thing. My input is "clean" and I want to retrieve "medium". Is there any way to fetch the key?
It would be :
const valueToSearch = "clean"
const result = Object.entries(priorityMap).find(entry => entry[1] === valueToSearch)[1]
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
{ "errors":
[
{ "value":"k#d.c",
"msg":"Email is not valid",
"param":"email",
"location":"body"
}
]
}
I wanna extract the msg from this json object. How do i do this?
const jsonContent = `{"errors":[{"value":"k#d.c","msg":"Email is not valid","param":"email","location":"body"}]}`;
const jsObject = JSON.parse(jsonContent);
const errorMessage = jsObject.errors[0].msg;
This will result in:
{
"errors":[
{
"value":"k#d.c",
"msg":"Email is not valid",
"param":"email",
"location":"body"
}
]
}
[UPDATE]
Sorry, didn't see you asked just for the msg property. Edited my code.
You can use JSON.parse(str) like so
var t = '{"errors":[{"value":"k#d.c","msg":"Email is not valid","param":"email","location":"body"}]}';
var msg = JSON.parse(t)["errors"][0]["msg"];
This question already has answers here:
JavaScript: Converting Array to Object
(2 answers)
Closed 8 years ago.
I've got the following array:
array = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]
i wanto convert it to json like this:
json = {{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}}
So than i can store everything in mongodb in a unique document.
Regards,
Just run a loop and equate...like...
var obj = {};
for(var i=0; i<array.length; i++)
{
obj[i] = array[i]
}
It will do
{
0:{"id":144,"price":12500000},
1:{"id":145,"price":13500000},
2:{"id":146,"price":13450000},
3:{"id":147,"price":11500000},
4:{"id":148,"price":15560000}
}
Because your JSON is invalid.
Not sure what you are asking
From array or another variable to json string =>
var str = JSON.stringify(thing);
From a json string to variable
var thing = JSON.parse(str);