Michal Szymanski
Michal Szymanski

Reputation: 23

Query language for JSON or how to use Javascript/Lua in Python?

I'm looking solution to query JSON that extracts specific data. This solution will be embedded inside our Python software (more exactly Python AWS Lambda) and the user should have the ability to specify such query string in GUI. For example, we have such JSON and I would extract "nazwisko_weterynarza.value" from "files" array if "typ_dokument" is equal to "szczepienie" and "data_szczepienia" is equal to 3434343. I've tried this using JSON Path but I've failed.
What about embedded Lua/Javascript in Python, but how to create a 'sandbox' to be sure that such a solution will be safe?

{
    "files" : 
        [
        {
            "meta":  {
                "updated": 555555,
                "attributes": [ 
                    {
                        "typ_dokumentu":
                            { 
                                "formula_type": "NONE",
                                "formula": "",
                                "value_type": "string",    
                                "value": "szczepienie",
                                "updated": 5345435435345
                            }
                    },
                    {
                        "data_szczepienia":
                        {
                            "formula_type": "NONE",
                            "formula": "",
                            "value_type": "int",    
                            "value": 3434343,
                            "updated": 5345435225345
                        },
                    },
                    {
                        "nazwisko_weterynarza":
                        {
                            "formula_type": "NONE",
                            "formula": "",
                            "value_type": "string",    
                            "value": "Nowak",
                            "updated": 5345435225345
                        }
                    }
                ]
            }
        },
        {
            "meta":  {
                "updated": 555555,
                "attributes": [ 
                    {
                        "typ_dokumentu" :
                        {
                            "formula_type": "NONE",
                            "formula": "",
                            "value_type": "string",    
                            "value": "Certyfikat urodzin",
                            "updated": 5345435435345
                        }
                    },
                    {
                        "data_urodzin" :
                        {
                            "formula_type": "NONE",
                            "formula": "",
                            "value_type": "int",    
                            "value": 8888888,
                            "updated": 5345435225345
                        }
                    }
                ]
            }
        }
    ]
}

Upvotes: 1

Views: 180

Answers (1)

wp78de
wp78de

Reputation: 18950

The AWS JSON (reference) path queries are not capable enough. Some more advanced JSONPath processors can do it, eg. using Jayway's JsonPath we could run a query like this:

$.files..meta.[?(@.attributes[*].typ_dokumentu.value contains 'szczepienie' && @.attributes[*].data_szczepienia.value contains '3434343')]..nazwisko_weterynarza.value

Reurns ["Nowak"] (You can try it online).

I don't know what you mean by embedded JavaScript/Lua, but in any event, you could create a web service that does the querying for you leveraging an implementation of your choice. Security wouldn't be an issue as long as your API is properly secured.

Upvotes: 1

Related Questions