JSON Output format

Dear friends,

I'm getting below API result and i would like to format them with Shell scripting.

Input

[
  {
    "id": 9,
    "description": "short desc",
    "name": "test",
    "name_with_namespace": "ABCD-PDFF-PLATFORM-TEST-V1 / test",
    "path": "test",
    "path_with_namespace": "ABCD-PDFF-platform-test-v1/test",
    "created_at": "2019-02-25T10:25:29.771Z",
    "default_branch": null,
    "tag_list": [],
    "ssh_url_to_repo": "git@114.114.56.5:ABCD-PDFF-platform-test-v1/test.git",
    "http_url_to_repo": "http://114.114.56.5/ABCD-PDFF-platform-test-v1/test.git",
    "web_url": "http://114.114.56.5/ABCD-PDFF-platform-test-v1/test",
    "readme_url": null,
    "avatar_url": null,
    "star_count": 0,
    "forks_count": 0,
    "last_activity_at": "2019-02-25T10:25:29.771Z",
    "namespace": {
      "id": 10,
      "name": "ABCD-PDFF-PLATFORM-TEST-V1",
      "path": "ABCD-PDFF-platform-test-v1",
      "kind": "group",
      "full_path": "ABCD-PDFF-platform-test-v1",
      "parent_id": null
    },
    "_links": {
      "self": "http://114.114.56.5/api/v4/projects/9",
      "issues": "http://114.114.56.5/api/v4/projects/9/issues",
      "merge_requests": "http://114.114.56.5/api/v4/projects/9/merge_requests",
      "repo_branches": "http://114.114.56.5/api/v4/projects/9/repository/branches",
      "labels": "http://114.114.56.5/api/v4/projects/9/labels",
      "events": "http://114.114.56.5/api/v4/projects/9/events",
      "members": "http://114.114.56.5/api/v4/projects/9/members"
    },
    "archived": false,
    "visibility": "internal",
    "resolve_outdated_diff_discussions": false,
    "container_registry_enabled": true,
    "issues_enabled": true,
    "merge_requests_enabled": true,
    "wiki_enabled": true,
    "jobs_enabled": true,
    "snippets_enabled": true,
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 3,
    "import_status": "none",
    "open_issues_count": 0,
    "public_jobs": true,
    "ci_config_path": null,
    "shared_with_groups": [],
    "only_allow_merge_if_pipeline_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "printing_merge_request_link_enabled": true,
    "merge_method": "merge",
    "external_authorization_classification_label": null,
    "permissions": {
      "project_access": null,
      "group_access": null
    },
    "repository_storage": "default",
    "approvals_before_merge": 0,
    "mirror": false,
    "packages_enabled": true
  },
  {
    "id": 6,
    "description": null,
    "name": "test",
    "name_with_namespace": "ABCD1 / test",
    "path": "test",
    "path_with_namespace": "ABCD-GROUPPDDFFPLATFORM/test",
    "created_at": "2019-02-21T07:44:14.083Z",
    "default_branch": null,
    "tag_list": [],
    "ssh_url_to_repo": "git@114.114.56.5:ABCD-GROUPPDDFFPLATFORM/test.git",
    "http_url_to_repo": "http://114.114.56.5/ABCD-GROUPPDDFFPLATFORM/test.git",
    "web_url": "http://114.114.56.5/ABCD-GROUPPDDFFPLATFORM/test",
    "readme_url": null,
    "avatar_url": null,
    "star_count": 0,
    "forks_count": 0,
    "last_activity_at": "2019-02-21T07:44:14.083Z",
    "namespace": {
      "id": 5,
      "name": "ABCD1",
      "path": "ABCD-GROUPPDDFFPLATFORM",
      "kind": "group",
      "full_path": "ABCD-GROUPPDDFFPLATFORM",
      "parent_id": null
    },
    "_links": {
      "self": "http://114.114.56.5/api/v4/projects/6",
      "issues": "http://114.114.56.5/api/v4/projects/6/issues",
      "merge_requests": "http://114.114.56.5/api/v4/projects/6/merge_requests",
      "repo_branches": "http://114.114.56.5/api/v4/projects/6/repository/branches",
      "labels": "http://114.114.56.5/api/v4/projects/6/labels",
      "events": "http://114.114.56.5/api/v4/projects/6/events",
      "members": "http://114.114.56.5/api/v4/projects/6/members"
    },
    "archived": false,
    "visibility": "private",
    "resolve_outdated_diff_discussions": false,
    "container_registry_enabled": true,
    "issues_enabled": true,
    "merge_requests_enabled": true,
    "wiki_enabled": true,
    "jobs_enabled": true,
    "snippets_enabled": true,
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 3,
    "import_status": "none",
    "open_issues_count": 0,
    "public_jobs": true,
    "ci_config_path": null,
    "shared_with_groups": [],
    "only_allow_merge_if_pipeline_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "printing_merge_request_link_enabled": true,
    "merge_method": "merge",
    "external_authorization_classification_label": null,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 50,
        "notification_level": 3
      }
    },
    "repository_storage": "default",
    "approvals_before_merge": 0,
    "mirror": false,
    "packages_enabled": true
  },
  {
    "id": 4,
    "description": "Who",
    "name": "applabonboarding",
    "name_with_namespace": "ABCD / applabonboarding",
    "path": "applabonboarding",
    "path_with_namespace": "ABCD/applabonboarding",
    "created_at": "2019-02-19T13:17:22.321Z",
    "default_branch": null,
    "tag_list": [],
    "ssh_url_to_repo": "git@114.114.56.5:ABCD/applabonboarding.git",
    "http_url_to_repo": "http://114.114.56.5/ABCD/applabonboarding.git",
    "web_url": "http://114.114.56.5/ABCD/applabonboarding",
    "readme_url": null,
    "avatar_url": null,
    "star_count": 0,
    "forks_count": 0,
    "last_activity_at": "2019-02-19T13:17:22.321Z",
    "namespace": {
      "id": 4,
      "name": "ABCD",
      "path": "ABCD",
      "kind": "group",
      "full_path": "ABCD",
      "parent_id": null
    },
    "_links": {
      "self": "http://114.114.56.5/api/v4/projects/4",
      "issues": "http://114.114.56.5/api/v4/projects/4/issues",
      "merge_requests": "http://114.114.56.5/api/v4/projects/4/merge_requests",
      "repo_branches": "http://114.114.56.5/api/v4/projects/4/repository/branches",
      "labels": "http://114.114.56.5/api/v4/projects/4/labels",
      "events": "http://114.114.56.5/api/v4/projects/4/events",
      "members": "http://114.114.56.5/api/v4/projects/4/members"
    },
    "archived": false,
    "visibility": "internal",
    "resolve_outdated_diff_discussions": false,
    "container_registry_enabled": true,
    "issues_enabled": true,
    "merge_requests_enabled": true,
    "wiki_enabled": true,
    "jobs_enabled": true,
    "snippets_enabled": true,
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 2,
    "import_status": "none",
    "open_issues_count": 0,
    "public_jobs": true,
    "ci_config_path": null,
    "shared_with_groups": [],
    "only_allow_merge_if_pipeline_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "printing_merge_request_link_enabled": true,
    "merge_method": "merge",
    "external_authorization_classification_label": null,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 50,
        "notification_level": 3
      }
    },
    "repository_storage": "default",
    "approvals_before_merge": 0,
    "mirror": false,
    "packages_enabled": true
  }
]

Output
ID | Description | path | last_activity_at
9 | short desc | test | 2019-02-25T10:25:29.771Z
6 | null | test | 2019-02-21T07:44:14.083Z
4 | Who | applabonboarding | 2019-02-19T13:17:22.321Z

Thank you

1 Like

Hi,

jq is an incredible JSON-tool. Here's a primer to start from:

script_producing_json | jq -r '.[] | [ (.id|tostring), .description, .path, .last_activity_at ] | join(" | ") '

# output:

9 | short desc | test | 2019-02-25T10:25:29.771Z
6 |  | test | 2019-02-21T07:44:14.083Z
4 | Who | applabonboarding | 2019-02-19T13:17:22.321Z

See: Stedolan jq

Hint: Have in mind, that a problem can get way to complex so that shell may not be an appropriate tool for the task and a decent scripting language(python, ruby,...) may serve you much better.

2 Likes

That's really Awesome @stomp. "jq" is new to me and i will explore on this.

Does this support sub level printing also? I would like to format the string highlighted in Blue color.

 "namespace": {
      "id": 4,
      "name": "ABCD",

Output:

ID | Description | path | last_activity_at | name
9 | short desc | test | 2019-02-25T10:25:29.771Z | ABCD-PDFF-PLATFORM-TEST-V1
6 | null | test | 2019-02-21T07:44:14.083Z | ABCD1
4 | Who | applabonboarding | 2019-02-19T13:17:22.321Z | ABCD

Thanks
Bala

Seems that this is easily possible too:

jq -r <data.json '.[] | [ (.id|tostring), .description, .path, .last_activity_at, .namespace.name ] |join(" | ") '

#output 

9 | short desc | test | 2019-02-25T10:25:29.771Z | ABCD-PDFF-PLATFORM-TEST-V1
6 |  | test | 2019-02-21T07:44:14.083Z | ABCD1
4 | Who | applabonboarding | 2019-02-19T13:17:22.321Z | ABCD

You are right. Thank you very much Stomp. I learnt today

In Javascript, FYI it would be something like (roughly, untested):

// put in array
var output = [];
jsonarray.forEach (element =>{
  output[] =  element.id + ' | ' + element.description + ' | ' + element.path + ' | ' + element.last_activity_at;
});
// put in string
var output = '';
jsonarray.forEach (element =>{
  output +=  element.id + ' | ' + element.description + ' | ' + element.path + ' | ' + element.last_activity_at + "\n";
});

etc etc... just two simple rough samples of the many ways to parsing JSON in Javascript .. it is easy.

1 Like

Hello stomp,

Nice solution in jq, could you please do let us know any documentation which we could follow for learning jq . Will be grateful to you.

Thanks,
R. Singh

The documentation on the mentioned JQ-Homepage: jq is quite nice and extensive. No special site I further know of.

...and as this is something, some person here may like, I am mentioning this:

jq is written in portable C, and it has zero runtime dependencies.

3 Likes