Cannot Parse json output: jq: parse error: Invalid numeric literal #827

Closed
opened 2024-02-10 23:19:19 +00:00 by xLogiiCx · 6 comments

What I'm trying to do and why

Hi, i am trying to write a Python Script to export Data about my Borg Repositories.
But i am having Problems parsing the output.

I tried following:

def get_repo_info(repository):
    # Build the borgmatic list command
    # repo_info_cmd = ['borgmatic', 'rinfo', '--json', '--repository', repository]
    cmd = f'borgmatic rinfo --json --repository {repository} | jq'
    sd = os.system(cmd)

    return sd

get_repo_info('/media/user/b/b.borg')

but i get following error:
jq: parse error: Invalid numeric literal at line 1, column 649

i also tried with subprocess and json.dumps but i get a simmilar error.
is there something wrong on my site?
i got a [ESC], % and some other weird things at the end of my json output string.

Steps to reproduce

No response

Actual behavior

No response

Expected behavior

No response

Other notes / implementation ideas

No response

borgmatic version

1.8.8

borgmatic installation method

AUR

Borg version

borg 1.2.7

Python version

3.11.7

Database version (if applicable)

No response

Operating system and version

No response

### What I'm trying to do and why Hi, i am trying to write a Python Script to export Data about my Borg Repositories. But i am having Problems parsing the output. I tried following: ```python def get_repo_info(repository): # Build the borgmatic list command # repo_info_cmd = ['borgmatic', 'rinfo', '--json', '--repository', repository] cmd = f'borgmatic rinfo --json --repository {repository} | jq' sd = os.system(cmd) return sd get_repo_info('/media/user/b/b.borg') ``` but i get following error: `jq: parse error: Invalid numeric literal at line 1, column 649` i also tried with subprocess and json.dumps but i get a simmilar error. is there something wrong on my site? i got a `[ESC]`, `%` and some other weird things at the end of my json output string. ### Steps to reproduce _No response_ ### Actual behavior _No response_ ### Expected behavior _No response_ ### Other notes / implementation ideas _No response_ ### borgmatic version 1.8.8 ### borgmatic installation method AUR ### Borg version borg 1.2.7 ### Python version 3.11.7 ### Database version (if applicable) _No response_ ### Operating system and version _No response_
Owner

Thanks for filing this, but so far I'm not able to reproduce it. Can I get a look at your borgmatic JSON output including the weird characters? If they're not showing up with copy and paste, feel free to redirect to a file and then attach the file here. And you can redact anything sensitive, of course. Thank you!

Thanks for filing this, but so far I'm not able to reproduce it. Can I get a look at your borgmatic JSON output including the weird characters? If they're not showing up with copy and paste, feel free to redirect to a file and then attach the file here. And you can redact anything sensitive, of course. Thank you!
Author
b'[{"cache": {"path": "/root/.cache/borg/red", "stats": {"total_chunks": 3808212, "total_csize": 1270077190113, "total_size": 1724789507361, "total_unique_chunks": 569956, "unique_csize": 92260417644, "unique_size": 107385743183}}, "encryption": {"mode": "repokey-blake2"}, "repository": {"id": "red", "last_modified": "2024-02-11T00:25:36.000000", "location": "/media/user/borg/pc.borg", "label": "local"}, "security_dir": "/root/.config/borg/security/red"}]\x1b[0m'

is what i get from following for example:

def get_info(repository):
    # Build the borgmatic info command
    cmd = ['borgmatic', 'rinfo', '--json', '--repository', repository]

    # Run the command and capture the output
    output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
    print(output)
    # Parse the JSON output
    data = json.loads(output)

    return data


get_info('/media/user/borg/pc.borg')

it kinda works with jq but there is still an Error:

def get_repo_info(repository):
    # Build the borgmatic list command
    # repo_info_cmd = ['borgmatic', 'rinfo', '--json', '--repository', repository]
    cmd = f'borgmatic rinfo --json --repository {repository} | jq'
    sd = os.system(cmd)
    
    return sd


get_repo_info('/media/user/borg/pc.borg')
jq: parse error: Invalid numeric literal at line 1, column 649
[
  {
    "cache": {
      "path": "/root/.cache/borg/red",
      "stats": {
        "total_chunks": 3808212,
        "total_csize": 1270077190113,
        "total_size": 1724789507361,
        "total_unique_chunks": 569956,
        "unique_csize": 92260417644,
        "unique_size": 107385743183
      }
    },
    "encryption": {
      "mode": "repokey-blake2"
    },
    "repository": {
      "id": "red",
      "last_modified": "2024-02-11T00:25:36.000000",
      "location": "/media/user/borg/pc.borg",
      "label": "local"
    },
    "security_dir": "/root/.config/borg/security/red"
  }
]

cmdline:

~ » sudo borgmatic rinfo --json --repository /media/user/borg/pc.borg > test.txt                                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
~ » cat test.txt                                                                                                                                                                                                                 
[{"cache": {"path": "/root/.cache/borg/red", "stats": {"total_chunks": 3808212, "total_csize": 1270077190113, "total_size": 1724789507361, "total_unique_chunks": 569956, "unique_csize": 92260417644, "unique_size": 107385743183}}, "encryption": {"mode": "repokey-blake2"}, "repository": {"id": "red", "last_modified": "2024-02-11T00:25:36.000000", "location": "/media/user/borg/pc.borg", "label": "local"}, "security_dir": "/root/.config/borg/security/red"}]%                                                      --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
~ »
```json b'[{"cache": {"path": "/root/.cache/borg/red", "stats": {"total_chunks": 3808212, "total_csize": 1270077190113, "total_size": 1724789507361, "total_unique_chunks": 569956, "unique_csize": 92260417644, "unique_size": 107385743183}}, "encryption": {"mode": "repokey-blake2"}, "repository": {"id": "red", "last_modified": "2024-02-11T00:25:36.000000", "location": "/media/user/borg/pc.borg", "label": "local"}, "security_dir": "/root/.config/borg/security/red"}]\x1b[0m' ``` is what i get from following for example: ``` def get_info(repository): # Build the borgmatic info command cmd = ['borgmatic', 'rinfo', '--json', '--repository', repository] # Run the command and capture the output output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) print(output) # Parse the JSON output data = json.loads(output) return data get_info('/media/user/borg/pc.borg') ``` it kinda works with jq but there is still an Error: ``` def get_repo_info(repository): # Build the borgmatic list command # repo_info_cmd = ['borgmatic', 'rinfo', '--json', '--repository', repository] cmd = f'borgmatic rinfo --json --repository {repository} | jq' sd = os.system(cmd) return sd get_repo_info('/media/user/borg/pc.borg') ``` ```json jq: parse error: Invalid numeric literal at line 1, column 649 [ { "cache": { "path": "/root/.cache/borg/red", "stats": { "total_chunks": 3808212, "total_csize": 1270077190113, "total_size": 1724789507361, "total_unique_chunks": 569956, "unique_csize": 92260417644, "unique_size": 107385743183 } }, "encryption": { "mode": "repokey-blake2" }, "repository": { "id": "red", "last_modified": "2024-02-11T00:25:36.000000", "location": "/media/user/borg/pc.borg", "label": "local" }, "security_dir": "/root/.config/borg/security/red" } ] ``` cmdline: ``` ~ » sudo borgmatic rinfo --json --repository /media/user/borg/pc.borg > test.txt -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ~ » cat test.txt [{"cache": {"path": "/root/.cache/borg/red", "stats": {"total_chunks": 3808212, "total_csize": 1270077190113, "total_size": 1724789507361, "total_unique_chunks": 569956, "unique_csize": 92260417644, "unique_size": 107385743183}}, "encryption": {"mode": "repokey-blake2"}, "repository": {"id": "red", "last_modified": "2024-02-11T00:25:36.000000", "location": "/media/user/borg/pc.borg", "label": "local"}, "security_dir": "/root/.config/borg/security/red"}]% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ~ » ```
Author

Got it woking by removing the ANSI from the String with a regex.
Maybe this is because of the Colored output?

def get_repo_info(repository):
    # Build the borgmatic list command
    cmd = ['borgmatic', '-nc', 'rinfo', '--json']

    rinfo = subprocess.check_output(cmd, text=True)
    regex = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
    result = regex.sub('', rinfo)
    rinfo_json = json.loads(result)

    return rinfo_json
Got it woking by removing the ANSI from the String with a regex. Maybe this is because of the Colored output? ```python3 def get_repo_info(repository): # Build the borgmatic list command cmd = ['borgmatic', '-nc', 'rinfo', '--json'] rinfo = subprocess.check_output(cmd, text=True) regex = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') result = regex.sub('', rinfo) rinfo_json = json.loads(result) return rinfo_json ```
Owner

Thanks for the additional details. I don't have a local repro still, but I think I know what might be going on. That code you're getting is a "styling reset" escape code, so even though colors are disabled automatically when --json is given, that reset code is still coming through for some reason. I'll mark this as a bug and look into suppressing the code.

Thanks for the additional details. I don't have a local repro still, but I think I know what might be going on. That code you're getting is a "styling reset" escape code, so even though colors are disabled automatically when `--json` is given, that reset code is still coming through for some reason. I'll mark this as a bug and look into suppressing the code.
witten added the
bug
label 2024-02-11 17:14:48 +00:00
Owner

Okay, this should hopefully be fixed in main and will be part of the next release!

Okay, this should hopefully be fixed in main and will be part of the next release!
Owner

Released in borgmatic 1.8.9!

Released in borgmatic 1.8.9!
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: borgmatic-collective/borgmatic#827
No description provided.