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"];
Related
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 )
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:
How to parse CSV data?
(14 answers)
Closed 8 years ago.
How do I take data in a json file given like this:
Ticker,Quandl Code,Name
STI,GOOG/NYSE_STI,SunTrust Banks
AAPL,GOOG/NASDAQ_AAPL,Apple Inc
and turn it into an array like this:
var stocks = [
["STI,GOOG/NYSE_STI,SunTrust Banks"],
["AAPL,GOOG/NASDAQ_AAPL,Apple Inc"]
];
Your input is in CSV, not JSON. The following question has a function that converts CSV for an array:
Javascript code to parse CSV data
you can do this :
var myarray = [];
var myJSON = "";
for (var i = 0; i < 10; i++) {
var item = {
"value": i,
"label": i
};
myarray.push(item);
}
myJSON = JSON.stringify({myarray: myarray});
alert(myJSON);