JSON Output format


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting JSON Output format
# 1  
JSON Output format

Dear friends,

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

Input
Code:
[
  {
    "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
This User Gave Thanks to baluchen For This Post:
# 2  
Hi,

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

Code:
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.

Last edited by stomp; 05-02-2019 at 11:03 AM..
These 2 Users Gave Thanks to stomp For This Post:
# 3  
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.

Code:
 "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
# 4  
Quote:
Does this support sub level printing also?
Seems that this is easily possible too:

Code:
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

# 5  
You are right. Thank you very much Stomp. I learnt today
# 6  
In Javascript, FYI it would be something like (roughly, untested):

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

Code:
// 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.
This User Gave Thanks to Neo For This Post:
# 7  
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
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #624
Difficulty: Medium
The continue statement is used in Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert any shell command output to JSON format?

Hi All, I am new to shell scripting, Need your help in creating a shell script which converts any unix command output to JSON format output. example: sample df -h command ouput : Filesystem size used avail capacity Mounted /dev/dsk/c1t0d0s0 8.1G 4.0G 4.0G 50% /... (13 Replies)
Discussion started by: balu1234
13 Replies

2. UNIX for Beginners Questions & Answers

Printing df -h output in json format

Hi All, i am trying to print the df -h ouput in json format. using below script. #!/usr/bin/env bash df -h > /tmp/sdf nawk '{print " "$1" "$2" "$3" "$4" "$5" "$6" "}' /tmp/sdf > /tmp/sdf1 nawk 'NR==1 { for (i=1; i<=NF; i++) { f = i }}{ print $(f), $(f), $(f), $(f), ... (2 Replies)
Discussion started by: sravani25
2 Replies

3. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

4. Shell Programming and Scripting

FORMAT output

Input File Symmetrix ID : 000192601507 Masking View Name : TS00P22_13E_1 Last updated at : 05:10:18 AM on Tue Mar 22,2011 Initiator Group Name : 10000000c960b9cd Host Initiators { WWN : 10000000c960b9cd WWN : 10000000c960b9dd } Port... (1 Reply)
Discussion started by: greycells
1 Replies

5. UNIX for Dummies Questions & Answers

How to format the output of df -h?

Hi, I need to format the output of the command "df -h": $ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rootvg-lv01 271G 13G 245G 6% / tmpfs 16G 0 16G 0% /dev/shm /dev/sda1 97M 50M 43M 55% /boot /dev/mapper/d1vg-lv01 124G 105G 13G 90% /d0 I used awk: df -h |... (6 Replies)
Discussion started by: seafan
6 Replies

6. Shell Programming and Scripting

perl code-sequence of json format

Hi All , Below is the perl code. from below code want to confirm one thing that wahtever the sequence of data we are passing through json format which contains 3 tuples of different sequences Eg: ParentID,SystemID,SendingTime,Time,ClientLocation,ClientID, ... (1 Reply)
Discussion started by: aish11
1 Replies

7. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

8. UNIX for Advanced & Expert Users

format df -k output

i am running df -k command on aix machine. i got the output like this. i need to store into file and send that file into microsoft excel.i need to allign properly. Filesystem 512 blocks Free % Used Iused %Iused Mounted on /dev/hd4 262144 126488 52% ... (9 Replies)
Discussion started by: wintercoat
9 Replies

9. Shell Programming and Scripting

Help in Getting specified output format

Hi all, I have input text file of this format objectclass:endeavor pid:12345 postalAddress:379 PROSPECT ST street:STE B l:TORRINGTON st:CT postalCode:067905238 telephoneNumber:9999999999... (2 Replies)
Discussion started by: pintoo
2 Replies

10. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

Featured Tech Videos