2

I'm a jq newbie, and I try to transform a json (a Swagger spec). I want to add an element to the array value of the "parameter" keys:

{
  ...
  "paths": {
    "/great/endpoint1": {
      "get": {
        "parameters": [] <<--- add a value here
       }
    }
    "/great/endpoint2": {
      "post": {
        "parameters": [] <<-- and here too here too etc.
  ....

The following jqplay almost works. It adds values to the right arrays, but it has the nasty side effect of also removing the "x-id" value from the root of the input json. It's probably because of a faulty if-condition. As the paths contain a varying string (the endpoint names), I don't know how to write a wildcard path expression to address those, which is why I have tried using walk instead: https://jqplay.org/s/az56quLZa3

1 Answer 1

1

Since the sample data is incomplete, it's difficult to say exactly what you're looking for but it looks like you should be using parameters in the call to walk:

walk(if type=="object" and has("parameters")
     then .parameters += [{"extra": "value"}]
     else . end)

If you want to restrict the walk to the top-level paths, you would preface the above with: .paths |=

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

1 Comment

Thanks! This works! Gosh, before posting here I spent hours trying to get the syntax for this right. When I couldn't, I tried the above variant with existence of key "schema" in the first element. Now it looks right and works right! Thanks!

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.