V: json names with whitespaces how to define structs

Created on 25 Sep 2019  Â·  14Comments  Â·  Source: vlang/v

V 0.1.20 a44a03f
Linux Mint 19.1

How do i decode this valid JSON RFC 4627 with the v json package

{"Request ID":"000000000178228"}

i tried

struct requestid {
arid string [json:"Request ID"]
}
is not working

nor does

struct requestid {
arid string [json:'Request ID']
}

nor does

struct requestid {
arid string [json: Request ID]
}

Bug

All 14 comments

I have no access to an current playground, but did you try:

struct requestid {
arid string [json:Request ID]
}

...when this does not work, V might have an issue when the JSON field name has an non-printable character

check()
next token = ]
v(+0x31025)[0x560a0241a025]
v(+0x3069e)[0x560a0241969e]
v(+0x2e22e)[0x560a0241722e]
v(+0x25dc7)[0x560a0240edc7]
v(+0x25744)[0x560a0240e744]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7ff10e84bb97]
v(+0x217a)[0x560a023eb17a]
arid string [json:Request ID]
^
/home/thomas/v/thomas/json.v:56:30: expected ] but got ID

I've never used/seen spaces in json keys and didn't realize that it's possible, sorry.

Will fix, it's an easy thing to fix.

.. this is in an answer json string from a restcall to a comercial software package ....

The only characters which need to be escaped are these:
grafik

Source: JSON

@schoko123

comercial software package

This does not mean a guarentee adhering to a certain standard - see MS vs. W3C :)

I fixed this on a branch yesterday.

There's one minor thing I need to do before I can release it.

I agree on gslicer but there is a lot of software out there which gives you json strings with white spaces in field names on REST Call Answers - so thank you medvednikov to fix this !!

https://jsonformatter.curiousconcept.com/
or
https://jsonformatter.org/

or any other json formatter on the web

put in { "white space test" : "white space test" }
and validate

as i understand the rfc 4627 a keyname is also a standard string as a value string and therefore whitespaces are allowed between two double quotes ...

@schoko123

as i understand the rfc 4627 a keyname is also a standard string as a value string and therefore whitespaces are allowed between two double quotes ...

yes indeed, the "field name" is a "string" thats why I posted the definition of string above where you see that simple spaces do not need to be escaped (but e.g. tabs need to)...

Hello!

Which branch did you fix the issue ?

Bye Thomas

Am Do., 26. Sept. 2019 um 16:17 Uhr schrieb Alexander Medvednikov <
[email protected]>:

I fixed this on a branch yesterday.

There's one minor thing I need to do before I can release it.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/vlang/v/issues/2097?email_source=notifications&email_token=ANFLMLNN4WVU4AJFJWQQLLTQLS76HA5CNFSM4I2I2YYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7VXEXA#issuecomment-535523932,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ANFLMLJ2SU4SL5NGI27IZEDQLS76HANCNFSM4I2I2YYA
.

@danieldaeschle @schoko123 it's working as spected not, can be closed?

Yes it works now, see testcode below.
Thanks

import json

struct User {
name string
age int
// If the field name is different in JSON, it can be specified
last_name string [json:'last Name']
}

data := '{ "name": "Frodo", "last Name": "Baggins", "age": 25 }'
user := json.decode(User, data) or {
eprintln('Failed to decode json')
return
}
println(user.name)
println(user.last_name)
println(user.age)

./jsontest
Frodo
Baggins
25

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markgraydev picture markgraydev  Â·  3Comments

elimisteve picture elimisteve  Â·  3Comments

ArcDrake picture ArcDrake  Â·  3Comments

lobotony picture lobotony  Â·  3Comments

PavelVozenilek picture PavelVozenilek  Â·  3Comments