-4

I want to change string to variable, please some one help me. this is a string >> {"method":"setValue","params":{"cmd_Motor":true}}

I wan to change like this:

3

2 Answers 2

0

Use JSON.parse()

const input = '{"method":"setValue","params":{"cmd_Motor":true}}';
const result = JSON.parse(input);
Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure if I understood your question, but you can convert a string to a JSON object and you can also convert a JSON object to a string:

converting from string to json

const myString = '{"method":"setValue","params":{"cmd_Motor":true}}';
const myJson = JSON.parse(myString);

console.log(myJson.method) // setValue
console.log(myJson.params.cmd_Motor) // true

converting from json to string

const myJson = { method: 'setValue', params: { cmd_Motor: true } };
const myString = JSON.stringify(myJson);

console.log(myString);

2 Comments

JSON is a string. So "converting from string to json" doesn't make sense
thank you so much, that is i want of result

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.