0

I have a few JSON files, the structure varies slightly (can have multiple "Columns")

but they look like this:

[{"data":"redacted1","type":"unknown"},{"data":"","type":"unknown"},{"data":"[email protected]","type":"email"}]

[{"data":"redacted2","type":"unknown"},{"data":"","type":"unknown"},{"data":"[email protected]","type":"email"}]

[{"data":"redacted3","type":"unknown"},{"data":"","type":"unknown"},{"data":"[email protected]","type":"email"}]

I want to replace the "type" with my own value, the third data pointer is email, the first one is name, and the middle one is junk, the third one already has a value.

So i want it to look like this:

[{"data":"redacted1","type":"name"},{"data":"","type":"junk"},{"data":"[email protected]","type":"email"}]

[{"data":"redacted2","type":"name"},{"data":"","type":"junk"},{"data":"[email protected]","type":"email"}]

[{"data":"redacted3","type":"name"},{"data":"","type":"junk"},{"data":"[email protected]","type":"email"}]

How can i do this with JQ?

1 Answer 1

1

Given

$ cat file.json
[{"data":"redacted1","type":"unknown"},{"data":"","type":"unknown"},{"data":"[email protected]","type":"email"}]

[{"data":"redacted2","type":"unknown"},{"data":"","type":"unknown"},{"data":"[email protected]","type":"email"}]

[{"data":"redacted3","type":"unknown"},{"data":"","type":"unknown"},{"data":"[email protected]","type":"email"}]

then

$ jq -c '.[0].type = "name" | .[1].type = "junk"' file.json
[{"data":"redacted1","type":"name"},{"data":"","type":"junk"},{"data":"[email protected]","type":"email"}]
[{"data":"redacted2","type":"name"},{"data":"","type":"junk"},{"data":"[email protected]","type":"email"}]
[{"data":"redacted3","type":"name"},{"data":"","type":"junk"},{"data":"[email protected]","type":"email"}]
1
  • exactly what i wanted. Thanks!
    – ilovejq
    Nov 2, 2020 at 2:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .