简体   繁体   中英

reciving json from rabbitmq in groovy

I have problem with parsing json provided by RabbitMq. When I print Rabbit message i got:

{id=546456, userId=345345}

I need to parse this json with JsonSlurper but i got exception:

groovy.json.JsonException: Unable to parse JSON object



     The current character read is '{' with an int value of 123
    expecting '}' or ',' but got current char 'i' with an int value of 105
    line number 1
    index number 1

It seems that quotes chars are missing. How to parse it without errors? Thanks

edit

When i send this message:

{'id':663558460,"userId":345345}

I recive this message in same form eg:

{'id':663558460,"userId":345345}

But there is another exception:

The current character read is ''' with an int value of 39
expecting '}' or ',' but got current char ''' with an int value of 39
line number 1
index number 1

this is not valid JSON: {id=546456, userId=345345}

neither is this: {'id':663558460,"userId":345345}

the JSON spec requires double-quotes for keys and string values.

this is valid JSON: {"id":663558460,"userId":345345}

note the double quotes around both keys. You need to adjust your code to send your JSON documents properly formatted

Sometimes, jq (the JSON Query processor) can be helpful with quasi-JSON input. In the particular case you give, if the quasi-JSON text is put in a file named invalid.json:

$ jq -n -f <(sed 's/=/:/g' invalid.json)
{
  "id": 546456,
  "userId": 345345
}

The above only works because in your case, substituting ":" for "=" blindly is satisfactory.

Even if that is not the case, however, chances are that the combination of jq and some "text-wrangling" will do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM