Trent
Trent

Reputation: 77

groovy.json.JsonException: expecting '}' or ',' but got current char

I am trying to get a piece of code working for me and not having much luck. So I have broken the code down to this little snippet that is causing me grief.

Can anyone help identify why in the world this error is happening?

import groovy.json.JsonSlurper;

String index = '[{accessCode=d20in9t, createdAt=2016-09-22T18:27:47.904Z, id=22cbf7c2-5d4e-4afe-a385-ddefb6e6dcf0, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=df04e5e4-b69a-4585-8a26-659a8f8ae15f}, readableId=npy5gcqnz8t, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T18:27:47.904Z}, {accessCode=fqcg0w9, createdAt=2016-09-22T18:27:47.904Z, id=74270a86-dfe8-4b58-82e7-080dd57ce57a, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=df04e5e4-b69a-4585-8a26-659a8f8ae15f}, readableId=3atchw5lhai, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T18:27:47.904Z}, {accessCode=o6eg9dp, createdAt=2016-09-22T18:27:47.904Z, id=8cc4f312-dae4-4daf-99cb-d060165ec8e8, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=df04e5e4-b69a-4585-8a26-659a8f8ae15f}, readableId=q9pqu7nm5oa, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T18:27:47.904Z}]'

def slurper = new groovy.json.JsonSlurper().parseText(index)
slurper.each {
    println(it)
}

http://ideone.com/3RrxyX

This is the error I get when I run it in my script ...

groovy.json.JsonException: expecting '}' or ',' but got current char 'a' with an int value of 97
           The current character read is 'a' with an int value of 97
          expecting '}' or ',' but got current char 'a' with an int value of 97
          line number 1
          index number 2
          [{accessCode=uvrbjeg, createdAt=2016-09-22T19:53:27.971Z, id=0328fce8-832d-499d-a19a-571ce19ce117, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=6c3b0b9f-fed0-4b62-a241-f42b896dc0ff}, readableId=5btnmmqe49m, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T19:53:27.971Z}, {accessCode=8fzwy2p, createdAt=2016-09-22T19:53:27.971Z, id=33db29b4-0e0d-449f-9ecf-f126dd745c87, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=6c3b0b9f-fed0-4b62-a241-f42b896dc0ff}, readableId=izs6wr742ea, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T19:53:27.971Z}, {accessCode=d4hjfue, createdAt=2016-09-22T19:53:27.971Z, id=8d49d092-3f2f-4801-85ae-aebca5d507d4, licenseType=mobileAppLicensesWithDevice, name=Lead Retrieval - Device Rental & App license, owner={entityType=exhibitor, id=6c3b0b9f-fed0-4b62-a241-f42b896dc0ff}, readableId=ur1onrasbd7, termId=630493a4-4a70-4f4f-afaf-31610c14c181, updatedAt=2016-09-22T19:53:27.971Z}]

Any ideas would be great to help out with this.

Upvotes: 3

Views: 12469

Answers (3)

Marcello DeSales
Marcello DeSales

Reputation: 22329

Not-well-formatted json

  • I had a python code generating a wrongly formatted python code where
"{"enabled": True}"
  • The problem was that the value was not a valid json code.
    • The "True" value should be with double-quotes.

The Groovy parser was getting confused about it and failing with the error above

Solutions

  • Make sure any json code is valid

Upvotes: 0

Evgeny Smirnov
Evgeny Smirnov

Reputation: 3016

You can change string format to correct JSON (key with quotes) or you can change JsonSlurper's parser type:

new JsonSlurper().setType(JsonParserType.LAX).parseText(index)

Upvotes: 3

tim_yates
tim_yates

Reputation: 171094

Json keys have to be in double quotes

All your values are strings, so they also need to be in double quotes.

Also you need "key":"value" instead of using =

String index = '[{"accessCode":"d20in9t", "createdAt":"2016-09-22T18:27:47.904Z", "id":"22cbf7c2-5d4e-4afe-a385-ddefb6e6dcf0", "licenseType":"mobileAppLicensesWithDevice", "name":"Lead Retrieval - Device Rental & App license", "owner":{"entityType":"exhibitor", "id":"df04e5e4-b69a-4585-8a26-659a8f8ae15f"}, "readableId":"npy5gcqnz8t", "termId":"630493a4-4a70-4f4f-afaf-31610c14c181", "updatedAt":"2016-09-22T18:27:47.904Z"}, {"accessCode:"fqcg0w9", "createdAt":"2016-09-22T18:27:47.904Z", "id":"74270a86-dfe8-4b58-82e7-080dd57ce57a", "licenseType":"mobileAppLicensesWithDevice", "name":"Lead Retrieval - Device Rental & App license", "owner":{"entityType":"exhibitor", "id":"df04e5e4-b69a-4585-8a26-659a8f8ae15f"}, "readableId":"3atchw5lhai", "termId":"630493a4-4a70-4f4f-afaf-31610c14c181", "updatedAt":"2016-09-22T18:27:47.904Z"}, {"accessCode":"o6eg9dp", "createdAt":"2016-09-22T18:27:47.904Z", "id":"8cc4f312-dae4-4daf-99cb-d060165ec8e8", "licenseType":"mobileAppLicensesWithDevice", "name":"Lead Retrieval - Device Rental & App license", "owner":{"entityType"="exhibitor", "id":"df04e5e4-b69a-4585-8a26-659a8f8ae15f"}, "readableId":"q9pqu7nm5oa", "termId":"630493a4-4a70-4f4f-afaf-31610c14c181", "updatedAt":"2016-09-22T18:27:47.904Z"}]'

Upvotes: 2

Related Questions