Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Hooray you saved my day Commented Aug 20, 2021 at 9:03
  • Certainly I will do that as your solution does work. I just found out that in my json, there are some cases when .[1] is not an array when it only has 1 record (very annoying), is there any workround to this situation. I use your code but getting jq: error (at <stdin>:25): Cannot iterate over string ("unixhost1123") Commented Aug 20, 2021 at 9:31
  • @HenryLiu an extension to my original answer that handles your nonconforming data Commented Aug 20, 2021 at 10:43
  • It should be enough to replace .[1][] with (.[1][]? // .[1]) in your original answer. Note the parentheses though. Commented Aug 20, 2021 at 10:53
  • 1
    And you seem to be doing well. I learned that using a set, as in the b key of { a:.[0], b:.[1][] }, was possible and reproduces the object once for each set member. That's rather neat actually. Commented Aug 20, 2021 at 10:57