Ruby Siren jsonquery

75 views Asked by At

My json content stored in a file:

{
    "vms": [
        {
            "component": "pgdb",
            "count": "1",
            "endpoints": "80:8080,5432:5432"
        },
        {
            "component": "mq",
            "count": "1",
            "endpoints": "80:8080,5672:5672,15672:15672"
        },
        {
            "component": "ucms",
            "count": "1",
            "endpoints": "80:80,8080:8080"
        },
        {
            "component": "wsgw",
            "count": "1",
            "endpoints": "8080:8080,9093:9093"
        }
    ]
}

in irb :

require 'json'
require 'Siren'

json = File.read('c:\\stack1.json')
irb(main):022:0> Siren.query "$.vms..[? @.component == ucms ]", json
=> []

I am trying to draft a query that will search on component name and return the value of endpoints. Any help/pointers will be appreciated.

Thanx

1

There are 1 answers

1
Edu Lomeli On

For string matching you need to use = operator, instead of ==. Also the file read needs to be parsed before the query:

json = Siren.parse File.read('c:\\stack1.json')
Siren.query "$.vms..[? @.component = ucms ]", json