0

I have a JSON of the below format in a textarea

{
    "name": "Stub",
    "request": {
        "method": "GET",
        "url": "/thing/1",
        "queryParameters": {
            "Accept": {
                "equalTo": "xm1"
            }
        }
    },
    "response": {
        "status": 200
    }
}

And as a part of my requirement I have a text box which has a onkeyup event which has to invoke a javascript function to replace the key param under queryParameters with the value that is newly typed in the text box

<input type="text" onkeyup="javascript:setFontText4(this.value);" size="10" id="Name1" name="Name1" value="Accept">

For example, if a user types "Change" in the textbox the newly constructed JSON should be as below

{
        "name": "Stub",
        "request": {
            "method": "GET",
            "url": "/thing/1",
            "queryParameters": {
                "Change": {
                    "equalTo": "xm1"
                }
            }
        },
        "response": {
            "status": 200
        }
    }

I wouldn't be sure if mutating would help here cause the "key" isn't static

I came across this nice example but I'm skeptical again cause the key isn't static

function setFontText4(text) {
    obj = { name: 'Bobo' }
    obj.somethingElse = obj.name
    delete obj.name

    document.getElementById("urls").innerHTML = JSON.stringify(obj, undefined, 4);
}
qatestprofile
  • 133
  • 13
  • `obj.request.queryParameters[text] = obj.request.queryParameters.Accept; delete obj.request.queryParameters.Accept;`, see linked question's answers for details. – T.J. Crowder Sep 07 '19 at 13:03
  • Hi, I will not be able to use "obj.request.queryParameters.Accept" for the reason being that value isn't constant, Once a user renames the key and if he wishes to rename again then the function wouldn't be able to find .Accept ! The linked question answers only for static values – qatestprofile Sep 07 '19 at 13:05
  • No, the linked question's answers are **not** just for static values; quite the opposite. If you need to, you can remember the old key's value in a variable so you know what it was next time: Outside your function: `var oldKey = "Accept";` Inside it: `obj.request.queryParameters[text] = obj.request.queryParameters[oldKey]; delete obj.request.queryParameters[oldKey]; oldKey = text;`. Or you can find the only key in the object via `Object.keys(obj.request.queryParameters)[0]`. – T.J. Crowder Sep 07 '19 at 13:13
  • Thanks, Getting there I think but I'm stuck with something http://jsfiddle.net/n0w5h9ds/ Based on the input from my textbox its supposed to update the value in the text area but that doesn't seem to work, the fiddle link is present, can you take a look ? – qatestprofile Sep 07 '19 at 16:24
  • You need to not do the replacement when the old key and the new key are the same. Also probably not when text is empty. So here's a swing at it: http://jsfiddle.net/tjcrowder/baepxtw3/ HTH – T.J. Crowder Sep 07 '19 at 16:32
  • Certainly, Thank you ! – qatestprofile Sep 07 '19 at 16:44

0 Answers0