0

I want to create a JSON object with input variable. I have a 2 input field. when I click button i want to take input values and make a JSON object with that value. I cannot create a JSON object with entered value.

I try that

//getting the value of input as a variable
let id=document.querySelector("#id");
let title=document.querySelector("#title");

first I try the below. it sends a blank value instead of variable string

var vObj= '{"id":'+id.value+',"title":'+title.value+'}'

I also try that to

var vObj = {"id":{},"v_title":{}, "v_category":{}};
vObj.id=id.value;
vObj.title=title.value;

tried that

let asd=id.value;
let bsd=title.value;

var vObj = {id:asd, title:bsd}

it returns a blank value instead of variable value.

i am making a mistake that the trigger button has typo. i couldn't see that. sorry

5
  • Are you sure document.querySelector("#id").value; document.querySelector("#title").value; is returning string value? Commented Oct 30, 2019 at 12:05
  • Do you really want to create JSON or a JavaScript object? What do you intend to do with vObj? The term "JSON object" is a misleading term so it needs clarification. Commented Oct 30, 2019 at 12:11
  • sorry, i forgot to change. i edit the question. Commented Oct 30, 2019 at 12:15
  • i need Json object to post rest api Commented Oct 30, 2019 at 12:15
  • So you really want to JSON. The answers you have received so far should help. Create an object and then use JSON.stringify to convert it to JSON. Commented Oct 30, 2019 at 12:24

2 Answers 2

3

You can create an object with name proeprties and then use JSON.stringify to turn this into valid JSON:

//let id = document.querySelector("#id").value;
let id = 'my_id';
//let title = document.querySelector("#title").value;
let title = 'my_title';
console.log('ID: ' + id);
console.log('Title: ' + title);

var vObj = { id: id, title: title };
var json = JSON.stringify(vObj);
console.log(json);

This line:

var vObj = { id: id, title: title };

Creates an object with an id with the value of the id variable, and a title with the value of the title variable.

This line:

var json = JSON.stringify(vObj);

Converts the object vObj to a JSON string.

Output:

ID: my_id
Title: my_title
{"id":"my_id","title":"my_title"}

Edit followimg @FelixKling comment

In ES6 it is possible to simplify the declaration of vObj using:

var vObj = {id, title};

This will create the same object with the assumed property names id and title. Obviously if your variable is not named id or title this will not work.

Sign up to request clarification or add additional context in comments.

7 Comments

FWIW, this can be written as var vobj = {id, title}; since ES6.
@FelixKling Thank you, a good point. Presumably that is only in cases like this where the variable name matches the object property name?
That's right. The nice thing about it is that it can prevent typos (assuming of course the variable name is correct in the first place). E.g. {titel: title} vs {title}.
@FelixKling Thanks for confirming. I'll update the answer to reflect this useful change
i m sorry, if i want to get input value with a variable -not static value- it returns like that => { id: '', title: '' }
|
1

First create an object, then stringify it to JSON.

var jsonObj = JSON.stringify( { "id" : id, "title" : title } );

2 Comments

i cannot create an object with input variable value. it returns blank like { id: ' ', title: ' ' }
Then your problem isn't with creating the object, it's with populating the input variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.