1

This is in continuation to this question and probably an advancement ( not a duplicate ) to this question

Below is my JSON

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

I would like to update the "equalTo" key to a value based on the select box

             <select id="Filter" onchange="javascript:setFontText5(this.value);" name="Filter">
                <option value=""></option>
                <option value="equalTo">equalTo</option>
                <option value="matches">matches</option>
                <option value="contains">contains</option>
              </select>

The Keys "Accept" and "EqualTo" that you see are dynamic so I cannot use something like this

obj.request.queryParameters.Accept.EqualTo[text]

I tried the below approach but it doesn't seem to work

 function setFontText5(text) {
        const str = document.getElementById("urls").value;
        const obj = JSON.parse(str);
        const oldKey = Object.keys(obj.request.queryParameters)[0];
        const oldKey1 = Object.keys(obj.request.queryParameters[oldKey])[0];
        if (text && oldKey1 !== text) {
          obj.request.queryParameters[oldKey][oldKey1][text] = obj.request.queryParameters[oldKey][oldKey1];
          delete obj.request.queryParameters[oldKey][oldKey1];
          document.getElementById("urls").innerHTML = JSON.stringify(obj, undefined, 4);
        }
      }
qatestprofile
  • 133
  • 13
  • What do you mean by "the Accept key" is dynamic? What does its value depend on? – user4520 Sep 08 '19 at 09:42
  • Hiya, Please read this [question](https://stackoverflow.com/questions/57833909/replace-key-param-for-dynamic-json-structure) The key value changes based on a text box, In continuation to that, the key ( equalTo, Matches ) within that key also should change based on a selection and that's why I'm calling it dynamic – qatestprofile Sep 08 '19 at 09:45
  • obj.request.queryParameters.Accept = {}; obj.request.queryParameters.Accept["EqualTo"]=text ? Replace key to that from select and set its value to some like "xml" here ? Or you can also use: delete obj.request.queryParameters.Accept.EqualTo; and create same or another again. – Jan Sep 08 '19 at 10:31
  • Hey Tom, I'm sorry if I have worded my question incorrectly but I cannot use ".Accept" cause this key isn't gonna be static rather dynamic. I need to update the key from "EqualTo" to "Contains" but without hardcoding ".Accept" – qatestprofile Sep 08 '19 at 10:36

1 Answers1

0

The Accept and equalTo keys can be replaced by replacing the objects :

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

var value = Object.values( Object.values( obj.request.queryParameters )[0] )[0]
var newKey = 'Placement', newKey1 = 'contains'

obj.request.queryParameters = { [newKey]: { [newKey1]: value } }

console.log( obj )

If it has to work in IE too :

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

var oldKey = Object.keys(obj.request.queryParameters)[0]
var oldKey1 = Object.keys(obj.request.queryParameters[oldKey])[0]
var value = obj.request.queryParameters[oldKey][oldKey1]
var newKey = 'Placement', newKey1 = 'contains'

obj.request.queryParameters = {}
obj.request.queryParameters[newKey] = {}
obj.request.queryParameters[newKey][newKey1] = value

console.log( obj )
Slai
  • 22,144
  • 5
  • 45
  • 53
  • Hey @Slai - I don't want to hardcode "Accept" cause at a different point in time the key would be "Placement" – qatestprofile Sep 08 '19 at 10:34
  • Do the same with Accept level 1st in this case obj.request.queryParameters = { [noAcceptBut]:filterSelection} But not sure if it will accept variable content, but if very nice construct... – Jan Sep 08 '19 at 10:40
  • @Slai : Working like a charm, Thank you ! – qatestprofile Sep 08 '19 at 10:54