I
I
Ismail2020-02-20 11:50:19
ruby
Ismail, 2020-02-20 11:50:19

Is it possible to get a hash storing a specific value in a huge JSON object?

Good time of the day.
There is a JSON with a huge number of hashes with nested hashes and arrays (which also have arrays and hashes, etc..).
Is it possible to create a specific algorithm that will pull a hash by value?
That is, for example, I need to get the "Farmer" value from the following JSON:

JSON

[{
        "id": 0,
        "firstName": "",
        "lastName": "",
        "middleName": null,
        "email": "",
        "phones": [
            null,
            null
        ],
        "groups": [{
            "id": 0,
            "name": ""
        }],
        "disabled": "",
        "technologies": [{
            "id": 0,
            "name": "",
            "children": [{
                "id": 1,
                "name": "",
                "children": [{
                    "id": 2,
                    "name": "Farmer",
                    "children": []
                }]
            }]
        }],
        "fullName": ""
    },
{
        "id": 0,
        "firstName": "",
        "lastName": "",
        "middleName": null,
        "email": "",
        "phones": [
            null,
            null
        ],
        "groups": [{
            "id": 0,
            "name": ""
        }],
        "disabled": "",
        "technologies": [{
            "id": 0,
            "name": "",
            "children": [{
                "id": 1,
                "name": "",
                "children": [{
                    "id": 2,
                    "name": "Not Farmer",
                    "children": []
                }]
            }]
        }],
        "fullName": ""
    }
]
# И таких хэшей много, все они разные по составу и т.д


I couldn't find any method/gem/algorithm on GitHub/StackOverFlow to do something like:
Ruby Pseudocode

data_hash = JSON.parse("filename.json")
data_hash.get_hash_by_value("Not Farmer")

#Output:
{
        "id": 0,
        "firstName": "",
        "lastName": "",
        "middleName": null,
        "email": "",
        "phones": [
            null,
            null
        ],
        "groups": [{
            "id": 0,
            "name": ""
        }],
        "disabled": "",
        "technologies": [{
            "id": 0,
            "name": "",
            "children": [{
                "id": 1,
                "name": "",
                "children": [{
                    "id": 2,
                    "name": "Not Farmer",
                    "children": []
                }]
            }]
        }],
        "fullName": ""
    }


I.e. You need to get ALL hashes that store a certain value ("In this example, "Not farmer")

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Mirilaczvili, 2020-02-21
@2ord

The data structure must be a predefined path, otherwise the result will be undefined.
Help:
Ruby #dig: https://stackoverflow.com/questions/34346653/how-d ...
https://goessner.net/articles/JsonPath/
.

S
Sergey Blokhin, 2020-02-20
@TITnet

https://gist.github.com/jodosha/58257#file-hash-rb

V
Valentin V., 2020-04-30
@tin_vsl

you can use JSONPath - something similar to xpath, allows you to query JSON at different levels of nesting
https://github.com/joshbuddy/jsonpath

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question