suvallur / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

Not able to use JSON Path for json attributes having space #376

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
I have a json in following format where the json property 'All Categories' has 
a space in the middle:
{
  "All Categories": {

    "color": "#0adc00",
    "name": "Category Main",
    "data": [
      {

        "color": "#00dc00",
        "name": "Sub Category 1",
        "comments": 13,
        "data": [
          {

            "color": "#00dc00",
            "name": "Sub Sub Category",
            "comments": 8,

          },
 ],

        "tagColor": "0X00dc00"
      },
 ],
    "comments": 59,
    "tagColor": "0X0adc00"
  },
 }
}

When i use the following json path expression in my code to get the data nodes
[All Categories].data 

Following exception is shown:
-----------------------------

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 27.
                            [All Categories].data
                             ^

1 error

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:194)
    at com.jayway.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:57)
    at com.jayway.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
    at com.jayway.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:32)
    at com.jayway.restassured.path.json.JsonPath.get(JsonPath.java:202)
    at com.jayway.restassured.path.json.JsonPath.getList(JsonPath.java:363)

--------------------------------------------------------------------------------
-
I also tried with following JSON Path expression which also didn't work:
All Categories.data 

Exception thrown for this instance is
--------------------------------------------------------------------------------
-
java.lang.IllegalArgumentException: The parameter "Categories" was used but not 
defined. Define parameters using the JsonPath.params(...) function
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:194)
    at com.jayway.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:54)
    at com.jayway.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
    at com.jayway.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:32)
    at com.jayway.restassured.path.json.JsonPath.get(JsonPath.java:202)
    at com.jayway.restassured.path.json.JsonPath.getList(JsonPath.java:363)
--------------------------------------------------------------------------------
-

Original issue reported on code.google.com by rpan...@gmail.com on 28 Nov 2014 at 9:51

GoogleCodeExporter commented 9 years ago
By using "\"All Categories\".data" i am able to get the nodes. This solution 
was given in https://code.google.com/p/rest-assured/issues/detail?id=318

Original comment by rpan...@gmail.com on 28 Nov 2014 at 9:57

GoogleCodeExporter commented 9 years ago
Yeah you need to escape the path if they contains space. It's a bit more 
readable if you use single quotes:

"'All Categories'.data"

Original comment by johan.ha...@gmail.com on 29 Nov 2014 at 8:54