简体   繁体   中英

Python - regex to get dynamic usernames from JSON text

I am trying to extract value of 'login' from a dump of JSON which is in the form of text (response.text)

Here's the string:

{
   "name":"master",
   "commit":{
      "sha":"adc3208a9ac76262250a",
      "commit":{
         "author":{
            "name":"root",
            "email":"dan.ja@foo.ca",
            "date":"2018-02-26T20:14:41Z"
         },
         "committer":{
            "name":"GitHub Enterprise",
            "date":"2018-02-26T20:14:41Z"
         },
         "message":"Update README.md",
         "tree":{
            "sha":"3e4710d0e021a0a7",
            "comment_count":0,
            "verification":{
               "verified":false,
               "reason":"unsigned",
               "signature":null,
               "payload":null
            }
         },
         "author":{
            "login":"kyle",
            "id":5
         }

I am just trying to pull the value 'kyle' from the login in the last line. The value of 'kyle' can change as it can be a different login each time. Thus I need string in "login":"string"

Here's what I have right now but that only gets me "login" :

/"login"[^\a]*"/g

Never parse JSON with regex, use a JSON parser.

With :

Input file :

{
   "commit" : {
      "commit" : {
         "tree" : {
            "verification" : {
               "payload" : null,
               "verified" : false,
               "signature" : null,
               "reason" : "unsigned"
            },
            "sha" : "3e4710d0e021a0a7",
            "comment_count" : 0
         },
         "author" : {
            "id" : 5,
            "login" : "kyle"
         },
         "committer" : {
            "name" : "GitHub Enterprise",
            "date" : "2018-02-26T20:14:41Z"
         },
         "message" : "Update README.md"
      },
      "sha" : "adc3208a9ac76262250a"
   },
   "name" : "master"
}

Command :

$ jq '.commit.commit.author.login' file.json

Or via a script :

#!/usr/bin/env python3
import json

string = """
{ 
   "commit" : {
      "commit" : {
         "tree" : {
            "verification" : {
               "payload" : null,
               "verified" : false,
               "signature" : null,
               "reason" : "unsigned"
            },
            "sha" : "3e4710d0e021a0a7",
            "comment_count" : 0
         },
         "author" : {
            "id" : 5,
            "login" : "kyle"
         },
         "committer" : {
            "name" : "GitHub Enterprise",
            "date" : "2018-02-26T20:14:41Z"
         },
         "message" : "Update README.md"
      },
      "sha" : "adc3208a9ac76262250a"
   },
   "name" : "master"
}
"""

j = json.loads(string)
print(j['commit']['commit']['author']['login'])

Output :

"kyle"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM