0

I'm trying send form input data to REST SERVICE. Here the Present format is

{
  "locationname":"test",
  "locationtype":"test",
  "address":"test"
}

but service accepting format is

 {
    "value": "{ locationname: test ,locationtype: test, address:test }",
 }

tried to convert string with below

const tests = JSON.parse(JSON.stringify(Form.value));

but how to assign to Value

I expect the result after form submit

{
  "value":"{ locationname: test ,locationtype: test, address:test }",
}
6
  • 1
    const tests = JSON.stringify({ value : JSON.stringify(Form.value) }); Commented Mar 28, 2019 at 11:48
  • @ Pranav C Balan Assign to value is done but parameters are still in string format { "value":"{ "locationname": "test" ,"locationtype": "test", "address":"test" }", } Commented Mar 28, 2019 at 12:09
  • if you need an object with property value as JSON string then use const tests = { value : JSON.stringify(Form.value) }; Commented Mar 28, 2019 at 12:10
  • It is sending as [object object] Commented Mar 28, 2019 at 12:14
  • your requirement is unclear Commented Mar 28, 2019 at 12:16

1 Answer 1

2

maybe this fits your requirement. The "modifiedJsonObject" should be what you need to submit.

const formValue = JSON.parse('{"locationname":"test","locationtype":"test","address":"test"}');
    const formValueString = JSON
      .stringify(formValue)
      .replace(/"/g, '');
    const modifiedJsonObject = { value: formValueString };
    const jsonString = JSON.stringify(modifiedJsonObject);

    // jsonString = '{"value":"{locationname:test,locationtype:test,address:test}"}'
Sign up to request clarification or add additional context in comments.

2 Comments

How to print values in html , end point giving response as ["id": 1, '{"value":"{name:test,type:test,address:test}"}' , "id":2, '{"value":"{name:test1,type:test1,address:test1}"}'];
First the given response represents an array of elements. Also it isn't a valid json. So you have to parse the whole string and get the string representing the value.

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.