You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by James McMahon <js...@gmail.com> on 2022/06/06 15:35:43 UTC

Misformatting json output

I am having problems properly formatting Json output to requirements.
I have three fields, similar to this:
fieldA has value {"lname":"Smith", "fname":"John"}
fieldB has value  {"age":"21", "gender":"M"}
fields has value 12345

I put each into an empty Groovy map, result[:] .

I'm required to generate Json output like this:
{
"fieldA": {"lname":"Smith", "fname":"John"},
"fieldB": {"age":"21", "gender":"M"},
"fieldC":"12345"
}

but am instead getting output with double quotes around the { } for fields
A and B, like this:
{
"fieldA": "{"lname":"Smith", "fname":"John"}",
"fieldB": "{"age":"21", "gender":"M"}",
"fieldC":"12345"
}

Is there a way I can suppress the surrounding of the values in the map when
I create the json?

Currently this is how I process my map to json:

def JsonStr = JsonOutput.prettyPrint(JsonOutput.toJson(result))

which I then write to my outputStream like this:

outputStream.write(JsonStr.getBytes(StandardCharsets.UTF_8))

Thanks in advance for any help.

Re: Re: Misformatting json output

Posted by "Nelson, Erick" <Er...@hdsupply.com>.
My example converts a java map (of maps and a number) to a json string.
Variable data is a map.
fieldA, a key in the data map, contains another map…etc.
I don’t know what your source is nor do I know the method in which you are attempting to produce your json.

From: James McMahon <js...@gmail.com>
Date: Monday, June 6, 2022 at 8:53 AM
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject: [EXT] Re: Misformatting json output
fieldC is a hash value, a mix of letters and numbers. It is not shown surrounded by quotes as incoming data, but I could use a toString() or equivalent in Groovy to make it a string in advance of my Json formatting, if that helps matters.

One difference I note is that in your data[ ] map, each of the fieldA and fieldB values you show is surrounded by [ and ] , while mine are surrounded by { and }. Could that be causing my challenge? Does that matter?

On Mon, Jun 6, 2022 at 11:46 AM Nelson, Erick <Er...@hdsupply.com>> wrote:
import groovy.json.*

def data = [
  fieldA:[lname:"Smith", fname:"John"],
  fieldB:[age:21, gender:"M"],
  fieldC:12345
]

println JsonOutput.toJson(data)


^^^ this works for me.
Running it produces this…

{"fieldA":{"lname":"Smith","fname":"John"},"fieldB":{"age":21,"gender":"M"},"fieldC":12345}

What is the source of your data look like?
Is fieldC a number or a string?

From: James McMahon <js...@gmail.com>>
Date: Monday, June 6, 2022 at 8:36 AM
To: users@groovy.apache.org<ma...@groovy.apache.org> <us...@groovy.apache.org>>
Subject: [EXT] Misformatting json output
I am having problems properly formatting Json output to requirements.
I have three fields, similar to this:
fieldA has value {"lname":"Smith", "fname":"John"}
fieldB has value  {"age":"21", "gender":"M"}
fields has value 12345

I put each into an empty Groovy map, result[:] .

I'm required to generate Json output like this:
{
"fieldA": {"lname":"Smith", "fname":"John"},
"fieldB": {"age":"21", "gender":"M"},
"fieldC":"12345"
}

but am instead getting output with double quotes around the { } for fields A and B, like this:
{
"fieldA": "{"lname":"Smith", "fname":"John"}",
"fieldB": "{"age":"21", "gender":"M"}",
"fieldC":"12345"
}

Is there a way I can suppress the surrounding of the values in the map when I create the json?

Currently this is how I process my map to json:

def JsonStr = JsonOutput.prettyPrint(JsonOutput.toJson(result))

which I then write to my outputStream like this:

outputStream.write(JsonStr.getBytes(StandardCharsets.UTF_8))

Thanks in advance for any help.

Re: Misformatting json output

Posted by James McMahon <js...@gmail.com>.
fieldC is a hash value, a mix of letters and numbers. It is not shown
surrounded by quotes as incoming data, but I could use a toString() or
equivalent in Groovy to make it a string in advance of my Json formatting,
if that helps matters.

One difference I note is that in your data[ ] map, each of the fieldA and
fieldB values you show is surrounded by [ and ] , while mine are surrounded
by { and }. Could that be causing my challenge? Does that matter?

On Mon, Jun 6, 2022 at 11:46 AM Nelson, Erick <Er...@hdsupply.com>
wrote:

> import groovy.json.*
>
>
>
> def data = [
>
>   fieldA:[lname:"Smith", fname:"John"],
>
>   fieldB:[age:21, gender:"M"],
>
>   fieldC:12345
>
> ]
>
>
>
> println JsonOutput.toJson(data)
>
>
>
>
>
> ^^^ this works for me.
>
> Running it produces this…
>
>
>
>
> {"fieldA":{"lname":"Smith","fname":"John"},"fieldB":{"age":21,"gender":"M"},"fieldC":12345}
>
>
>
> What is the source of your data look like?
>
> Is fieldC a number or a string?
>
>
>
> *From: *James McMahon <js...@gmail.com>
> *Date: *Monday, June 6, 2022 at 8:36 AM
> *To: *users@groovy.apache.org <us...@groovy.apache.org>
> *Subject: *[EXT] Misformatting json output
>
> I am having problems properly formatting Json output to requirements.
>
> I have three fields, similar to this:
>
> fieldA has value {"lname":"Smith", "fname":"John"}
>
> fieldB has value  {"age":"21", "gender":"M"}
>
> fields has value 12345
>
>
>
> I put each into an empty Groovy map, result[:] .
>
>
>
> I'm required to generate Json output like this:
>
> {
>
> "fieldA": {"lname":"Smith", "fname":"John"},
>
> "fieldB": {"age":"21", "gender":"M"},
>
> "fieldC":"12345"
>
> }
>
>
>
> but am instead getting output with double quotes around the { } for fields
> A and B, like this:
>
> {
>
> "fieldA": "{"lname":"Smith", "fname":"John"}",
>
> "fieldB": "{"age":"21", "gender":"M"}",
>
> "fieldC":"12345"
>
> }
>
>
>
> Is there a way I can suppress the surrounding of the values in the map
> when I create the json?
>
>
>
> Currently this is how I process my map to json:
>
>
>
> def JsonStr = JsonOutput.prettyPrint(JsonOutput.toJson(result))
>
>
>
> which I then write to my outputStream like this:
>
>
>
> outputStream.write(JsonStr.getBytes(StandardCharsets.UTF_8))
>
>
>
> Thanks in advance for any help.
>

Re: Misformatting json output

Posted by "Nelson, Erick" <Er...@hdsupply.com>.
import groovy.json.*

def data = [
  fieldA:[lname:"Smith", fname:"John"],
  fieldB:[age:21, gender:"M"],
  fieldC:12345
]

println JsonOutput.toJson(data)


^^^ this works for me.
Running it produces this…

{"fieldA":{"lname":"Smith","fname":"John"},"fieldB":{"age":21,"gender":"M"},"fieldC":12345}

What is the source of your data look like?
Is fieldC a number or a string?

From: James McMahon <js...@gmail.com>
Date: Monday, June 6, 2022 at 8:36 AM
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject: [EXT] Misformatting json output
I am having problems properly formatting Json output to requirements.
I have three fields, similar to this:
fieldA has value {"lname":"Smith", "fname":"John"}
fieldB has value  {"age":"21", "gender":"M"}
fields has value 12345

I put each into an empty Groovy map, result[:] .

I'm required to generate Json output like this:
{
"fieldA": {"lname":"Smith", "fname":"John"},
"fieldB": {"age":"21", "gender":"M"},
"fieldC":"12345"
}

but am instead getting output with double quotes around the { } for fields A and B, like this:
{
"fieldA": "{"lname":"Smith", "fname":"John"}",
"fieldB": "{"age":"21", "gender":"M"}",
"fieldC":"12345"
}

Is there a way I can suppress the surrounding of the values in the map when I create the json?

Currently this is how I process my map to json:

def JsonStr = JsonOutput.prettyPrint(JsonOutput.toJson(result))

which I then write to my outputStream like this:

outputStream.write(JsonStr.getBytes(StandardCharsets.UTF_8))

Thanks in advance for any help.