I have this code:
JavaScript
x
3
1
let test = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}';
2
console.log(JSON.parse(test));
3
It of course fail because in values
I have an object. Is there any option how to parse this string in easy way? Or is it not possible at all?
At the end the result should be:
JavaScript
1
9
1
{
2
attribute_as: 'plan_id',
3
operator: 'fromTo',
4
values: {
5
from: 70,
6
to: 80
7
}
8
}
9
Advertisement
Answer
The string is incorrect:
JavaScript
1
16
16
1
let err = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}';
2
// This is the original string
3
let pass = '{"attribute_as":"plan_id","operator":"fromTo","values":{"from":70,"to":80}}';
4
// Corrected string
5
6
let desiredObj = {
7
attribute_as: 'plan_id',
8
operator: 'fromTo',
9
values: {
10
from: 70,
11
to: 80
12
}
13
};
14
15
console.log(JSON.stringify(desiredObj) == err);
16
console.log(JSON.stringify(desiredObj) == pass);