Answer the question
In order to leave comments, you need to log in
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:
[{
"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": ""
}
]
# И таких хэшей много, все они разные по составу и т.д
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": ""
}
Answer the question
In order to leave comments, you need to log in
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/
.
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 questionAsk a Question
731 491 924 answers to any question