-1

I have the following JSON-array from a json-file:

[
  {
    "key": "Ley1",
    "file": "Filepath1",
    "line": 10,
    "rule": "csharpsquid:S1643",
    "message": "Use a StringBuilder instead.",
    "type": "CODE_SMELL"
  },
  {
    "key": "Key2",
    "file": "FilePath2",
    "line": 12,
    "rule": "csharpsquid:S1643",
    "message": "Use a StringBuilder instead.",
    "type": "CODE_SMELL"
  }
]

and I want to add the variable "critical" to all items of it using a bash-command so it looks like this:

[
      {
        "key": "Key1",
        "file": "Filepath1",
        "line": 10,
        "rule": "csharpsquid:S1643",
        "message": "Use a StringBuilder instead.",
        "type": "CODE_SMELL",
        "critical": "No"
      },
      {
        "key": "Key2",
        "file": "FilePath2",
        "line": 12,
        "rule": "csharpsquid:S1643",
        "message": "Use a StringBuilder instead.",
        "type": "CODE_SMELL",
        "critical": "Yes"
      }
    ]

Unfortunately I am a complete JSON and bash beginner and could not find a bash-command solving this problem. I tried a litte bit with jq on jq-play but it did not really lead to something so I wanted to try it here. I hope this are all information needed, so does anyone know maybe a command for this?

EDIT: This here worked, thanks!

.[] |= . + {"critical": "No"}

But no i want to determine the value of "yes" or "no" depending on the file-value. Do you know how to edit the command so it checks the file-value in order to determine the critical-value?

It should decide like this:

Filepath1, Filepath3 leads to "critical" : "No"

and

Filepath2, Filepath4, Filepath5 leads to "critical" : "Yes"

puffel
  • 1
  • 1
  • 3
    How is the value of Yes or No determined? Also please post the attempts made – Inian Dec 08 '20 at 11:08
  • 1
    Does this answer your question? [Add new element to existing JSON array with jq](https://stackoverflow.com/questions/42245288/add-new-element-to-existing-json-array-with-jq) – 0stone0 Dec 08 '20 at 11:10
  • `.[] |= . + {"critical": "No"}` – 0stone0 Dec 08 '20 at 11:10
  • Unfortunatles the question of the "Add new element to existing JSON array with jq " was about adding an item to an array, not a variable to the entries of an array The value of yes or no depends on which filepath it is. So in filepath1, it should be no and in filepath 2 yes but since i only have like five paths available I would just want to check what the path value is and if it is from 1,2,3,4 or 5 and then if it is critical or not. – puffel Dec 08 '20 at 11:16
  • @puffel: Your claim seems to contradicting, you said `filepath1` should be no, but later said if the path is `1,2,3,4 or 5`, then critical yes ? – Inian Dec 08 '20 at 11:21
  • oh sorry! this was confusing. Filepath1 or Filepath3 -> critical: no Filepath2 or Filepath 4 or Filepath5 -> critical: yes – puffel Dec 08 '20 at 11:23
  • Thank you all so much, it finally works!! – puffel Dec 08 '20 at 11:56

2 Answers2

0

You can use an jq if-statement;

jq '.[] |= . + {"critical": (if .file == "FilePath1" or .file == "FilePath3" then "no" else "yes" end)}'

Try it on jqPlay

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    Glad it worked! Please take a look at [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers), please accept/upvote any answers that have helped! – 0stone0 Dec 08 '20 at 12:02
0

To minimize redundancy, you could go with:

map(. + {critical: (if .file | IN("FilePath1", "FilePath3") then "no" else "yes" end) })

Or for brevity:

map(. + {critical: ((.file | select(IN("FilePath1", "FilePath3")) | "no") // "yes" )})
peak
  • 105,803
  • 17
  • 152
  • 177