jq headache

Have this structure of JSON:

(
    [0] => stdClass Object
        (
            [create_user] => Array
                (
                    [0] => stdClass Object
                        (
                            [time] => 2022-10-01_12:43:05
                            [level] => success
                            [message] => Username created: xbnitapwr
                        )

                )

        )

    [1] => stdClass Object
        (
            [create_user] => Array
                (
                    [0] => stdClass Object
                        (
                            [time] => 2022-10-01_12:43:05
                            [level] => success
                            [message] => User modified: xbnitapwr
                        )

                )

        )

)

I need to make it like this:

(
    [0] => stdClass Object
        (
            [create_user] => Array
                (
                    [0] => stdClass Object
                        (
                            [time] => 2022-10-01_12:43:05
                            [level] => success
                            [message] => Username created: xbnitapwr
                        )
                    [1] => stdClass Object
                        (
                            [time] => 2022-10-01_12:43:05
                            [level] => success
                            [message] => User modified: xbnitapwr
                        )
                )
        )
)

TL;DR: I need to group elements by create_user key. And this is in bash env with jq. Any suggestions?

Tagged:

Comments

  • idk, just python, is already installed on most linux distros.

  • I know jq well.
    jq deals with JSON, not PHP print_r.
    Hence, you need to write your snippets in actual JSON.

    NVMe2G for life!
    You can also consider Webhosting24 or LevelOne or Spartan. (paid links)

  • I've long since concluded jq is witchcraft so usually do same as Neoon. Hopefully someone else can help, cause def seems like something that is doable with bash & jq

  • ehabehab Content Writer
    edited October 1

    one advise about

    jq "some filter" | jq "other filter" | jq "the clit"

    until you get it.

    as for env vars you'll need to bash script and test first.

  • ehabehab Content Writer

    btw, are you sure inputs are vaild jsons.

  • I just presented output from php :) . Json is really valid. Have some mumbo jumbo in bash wich send info to php backend api. I guess it is more doable in php side...

  • I'm glad I'm not the only person who thinks jq is witchcraft.

    Get the best deal on your next VPS or Shared/Reseller hosting from RacknerdTracker.com - The original aff garden.

  • @dahartigan said:
    I'm glad I'm not the only person who thinks jq is witchcraft.

    Ou, just start using some awk magic with more advanced syntax. I can spend 8 hours straight mangling and searching for awk goodness. I guess depends on level of masochizm - you enjoy the process.

    Thanked by (1)dahartigan
  • edited October 3

    Ok... Trying to convert bash script into python. Immediately faced the fact how python is disgusting for me. Any suggestions how to solve this?

    #!/usr/bin/env python3
    
    import subprocess as sp
    import shlex
    import argparse
    import json
    from configparser import ConfigParser
    
    parser = argparse.ArgumentParser()
        parser.add_argument(
            '-url', type=str, help="(str) Site URL", nargs=1, required=True)
        args = parser.parse_args()
    
        url = args.url
        about = "This is URL:"
    
        print(f"{about} {url}")
    

    Result of this madness is:

    This is URL: ['url.domain.tld']

    I assume this args.url is a list object. Why the surounding [' '] , I need bare string.

  • NVM, resolved with this:

    url = args.url[0]

    But for me this is insane... I need to look for another tool.

  • Is PHP an option? You said it was easy for you in PHP.

    In jq, I think you'd want to construct the main list using the recursive descent filter (..).

  • @mwt said:
    Is PHP an option? You said it was easy for you in PHP.

    In jq, I think you'd want to construct the main list using the recursive descent filter (..).

    Yes, PHP I understand and syntax is human readable. Thank you for suggestion.

Sign In or Register to comment.