kubernetes / website

Kubernetes website and documentation repo:
https://kubernetes.io
Creative Commons Attribution 4.0 International
4.36k stars 14.11k forks source link

JSONPath Support #46848

Open VipinVKKariyad opened 3 weeks ago

VipinVKKariyad commented 3 weeks ago

we need to get exact path of a sub object so need to add this command in documentation or need a method for json path

kubectl get nodes -o=json | jq -r '
def traverse(level):
    . as $in
    | if type == "object" then
        to_entries | map(
            "##" + (level | tostring) + " " + .key + ": " + (
                if .value | type == "object" or type == "array" then
                    "\n" + (.value | traverse(level + 1))
                else
                    .value | tostring
                end
            )
        ) | .[]
    elif type == "array" then
        map("##" + (level | tostring) + " " + (. | tostring) + (
            if type == "object" or type == "array" then
                "\n" + (. | traverse(level + 1))
            else
                ""
            end
        )) | .[]
    else
        "##" + (level | tostring) + " " + (. | tostring)
    end;
.items[] | traverse(1)'

op will be

1 status:

2 nodeInfo:

3 osImage: Ubuntu 20.04.5 LTS

k8s-ci-robot commented 3 weeks ago

This issue is currently awaiting triage.

SIG Docs takes a lead on issue triage for this website, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
dipesh-rawat commented 3 weeks ago

Hi @VipinVKKariyad Could you please provide the URL of the page in the issue where you are suggesting an improvement?

/triage needs-information

Ritikaa96 commented 2 weeks ago

we need to get exact path of a sub object so need to add this command in documentation or need a method for json path

kubectl get nodes -o=json | jq -r '
def traverse(level):
    . as $in
    | if type == "object" then
        to_entries | map(
            "##" + (level | tostring) + " " + .key + ": " + (
                if .value | type == "object" or type == "array" then
                    "\n" + (.value | traverse(level + 1))
                else
                    .value | tostring
                end
            )
        ) | .[]
    elif type == "array" then
        map("##" + (level | tostring) + " " + (. | tostring) + (
            if type == "object" or type == "array" then
                "\n" + (. | traverse(level + 1))
            else
                ""
            end
        )) | .[]
    else
        "##" + (level | tostring) + " " + (. | tostring)
    end;
.items[] | traverse(1)'

op will be

1 status: ##2 nodeInfo: ##3 osImage: Ubuntu 20.04.5 LTS

I do not understand the link mentioned in ##1, ##2, ##3 seems like mistakenly added. However the code stated just convert the json data in different format and content remains the same. If you are trying to find subobject or subresource the client-go can help.