1

I had a look around but I cannot find anything that helped, here my problem, I have a bash script that make a curl query and get a json, that look like that:

{
  "user1": {
    "email": "[email protected]",
    "shell": "/usr/local/bin/bash",
    "status": "ACTIVE",
    "userId": "user1",
    "homeDir": "/home/user1",
    "unixUid": 123456,
    "accesses": [
      "computer1"
    ],
    "personId": 1234,
    "isPrimary": true,
    "projectGroupIds": [
      "bigproj"
    ],
    "primaryProjectGroupId": "bigproj"
  },
  "user2": {
    "email": "[email protected]",
    "shell": "/usr/local/bin/bash",
    "status": "ACTIVE",
    "userId": "user2",
    "homeDir": "/home/user2",
    "unixUid": 9876564,
    "accesses": [
      "computer1"
    ],
    "personId": 5678,
    "isPrimary": true,
    "projectGroupIds": [
      "smallporj"
    ],
    "primaryProjectGroupId": "smallproj"
...

What I need to do is to create a list, that contains:

UserId: user1  email: [email protected]
UserId: user2  email: [email protected]
UserId: user3  email: [email protected]
...

My problem is that the username key, is basically always changing and it doesn't have a fix key name. The top key types are:

jq -c 'to_entries[] | [.key, (.value|type)]'
["meta","object"]
["body","object"]

So what should be the best approach to create a list that I'll need to compare with another one? Thanks in advance.

2 Answers 2

3

Would this work for you?

jq -c 'to_entries[] | "UserId: " + .key + "  email: " + .value.email '
Sign up to request clarification or add additional context in comments.

Comments

2

Another approach:

  keys_unsorted[] as $key
  | "UserId: \($key)  email: \(.[$key].email)"

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.