Search code examples
jsonjsonpath

JSON Path seems to be removing a comma


I'm using JSON Path to extract a some data from a json object.

The object looks like this:

{
   "properties":
      { 
         "random_field": "abc",
         "max value": "£35,900"
      }
}

My JSON path looks like this:

insert into display_paths (type,name,jsonpath) values ('type','Value (£)','$.properties.[''max value'']');

Annoyingly, this is returning the value without the ',', instead replacing it with a space:

Value (£): £35 900

Has anyone had a similar issue? Potentially it is a front end issue as opposed to a json path issue?


Solution

  • Note: Consider the risk of SQL injection when inserting unprepared data from external sources into an SQL statement like in your example. Read about prepared statements, using them can prevent this.


    About the jsonpath issue, looks like a quoting issue with your code. Check this working example:

    import json
    from jsonpath import jsonpath
    
    
    data = json.loads('''
    {
       "properties":
          { 
             "random_field": "abc",
             "max value": "£35,900"
          }
    }
    ''')
    
    
    query = "$.properties.['max value']"
    # Note that this works too
    query = "$.properties.[max value]"
    
    print(data)
    print(jsonpath(data, query))
    

    Output:

    {'properties': {'random_field': 'abc', 'max value': '£35,900'}}
    ['£35,900']
    

    PS: pip install jsonpath must be run for this example to work.