2

I have a json of the below format

{
    "name": "Stub",
    "request": {
        "method": "GET",
        "url": "/thing/1/4"
    },
    "response": {
        "status": 200
    }
}

I would like to add the below part to my json

"queryParameters": {
            "Accept": {
                "equalTo": "xml"
            }
        }

so my final outcome is of the below format

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

I tried the below but it doesn't seem to help, I also tried to parse which didn't help either

obj['theTeam'].push("queryParameters": { "Accept": { "equalTo": "xml" } });
jsonStr = JSON.stringify(obj);
1
  • push() is an array method but you are working with an object literal not array Commented Aug 6, 2019 at 18:24

2 Answers 2

3

Try

obj["request"]["queryParameters"] = { "Accept": { "equalTo": "xml" } }
Sign up to request clarification or add additional context in comments.

3 Comments

Or obj.request.queryParameters = ...
But wouldn't this need the queryParameters key to be present in the JSON already ?
No, it will create it or override an existing one.
0

I sugget to use your json like a object to access to the attributes into it:

// Try edit msg
var obj = {
    "name": "Stub",
    "request": {
        "method": "GET",
        "url": "/thing/1/4"
    },
    "response": {
        "status": 200
    }
};
var partToAdd = {
            "Accept": {
                "equalTo": "xml"
            }
};
obj.request.queryParameters = partToAdd;
console.log(obj);

Comments

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.