Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help with a query to get all paths in a nested json #337

Closed
shabbskagalwala opened this issue Nov 10, 2023 · 2 comments
Closed

help with a query to get all paths in a nested json #337

shabbskagalwala opened this issue Nov 10, 2023 · 2 comments

Comments

@shabbskagalwala
Copy link

First of all thank you for this awesome project! I am trying to implement this in one of our libraries to get values from nested objects. The problem is our json structure is not known, but the key names will always be the same. For example

{
  "us-west-2": {
    "dev": {
      "service1": {
        "cost": "float",
        "type": "string",
        "count": "int"
      },
      "service2": {
        "cost": "float",
        "type": "string",
        "count": "int"
      }
    },
    "prd": {
      "service1": {
        "cost": "float",
        "type": "string",
        "count": "int"
      }
    }
  },
  "us-east-1": {
    "dev": {
      "service1": {
        "cost": "float",
        "type": "string",
        "count": "int"
      },
      "service2": {
        "cost": "float",
        "type": "string",
        "count": "int"
      }
    },
    "prd": {
      "service1": {
        "cost": "float",
        "type": "string",
        "count": "int"
      }
    }
  }
}

Is there a way for me to get the values for the count and cost keys in each top level key us-west-2 and us-east-1. The problem is the json structure is dynamic, in some cases we'll have only us-west-2 in other cases there'll be no us-west-2 and many other values and similar for the nested keys as well. The only thing common will be

        "cost": "float",
        "type": "string",
        "count": "int"

Thank you!

@volans-
Copy link

volans- commented Nov 10, 2023

@shabbskagalwala
In recent gjson versions (I think since v1.16.0) there is the new @dig modifier (see SYNTAX.md#modifiers ) that combined with multipaths could be used for example like this:

{"count":@dig:count,"cost":@dig:cost}

to get (I've pretty-fied the output for easier readability):

{
  "count": ["int", "int", "int", "int", "int", "int"],
  "cost": ["float", "float", "float", "float", "float", "float"]
}

I'm not sure this is what you're looking for. If not, could you provide an example of output you are looking for?

@shabbskagalwala
Copy link
Author

Thank you so much - i should have read the docs! Appreciate your help :) This can be closed.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants