0

I have two packages running, one is written in python and the other in Java. The python package encodes a json(dictionary) object and sends it to the java package which decodes it into a specific POJO.

Let the json in python be:

{ "name": "John", "Age": 24, "Income": None }

The problem is that Java does not support None, it supports null, hence it is unable to parse the received json throwing error that "found None expected true, false, null, NaN".

Base64 encoding of above json in python:

eyAibmFtZSI6ICJKb2huIiwgIkFnZSI6IDI0LCAiSW5jb21lIjogTm9uZX0=

Base64 encoding of above json in java:

eyAibmFtZSI6ICJKb2huIiwgIkFnZSI6IDI0LCAiSW5jb21lIjogbnVsbH0=

I need to parse the null value in Java as well and cannot simply drop it from the json. What can be the possible solutions to avoid this error?

5
  • Sorry, but how is this related to Base64? Base64 is just an encoding and doesn't change the content. Commented Aug 25, 2021 at 16:58
  • None and null have different base 64 encodings Commented Aug 25, 2021 at 16:59
  • When you transfer the JSON directly, without encoding, the problem would be the same. Don't know what you expect from a base64 encode/decoder. Commented Aug 25, 2021 at 17:03
  • I want Java to recognize that there is null field Commented Aug 25, 2021 at 17:04
  • This is probably the dump of a Python dictionary object. You might want to use the actual JSON library for python (docs.python.org/3/library/json.html). Commented Aug 25, 2021 at 21:40

1 Answer 1

1

The problem is that { "name": "John", "Age": 24, "Income": None } is not a valid JSON object because None is not a valid JSON value.

According to the JSON Grammar a value may be:

  • a string ("xy")
  • a number (12.345)
  • another object ({ "prop": "value" })
  • an array ([1,2,3])
  • true
  • false
  • null

If your json encoder produces the value None it is broken - maybe you should use the standard python encode:

import json
r = {"name": "John", "Age": 24, "Income": None}
enc = json.JSONEncoder()
print(enc.encode(r))

produces valid JSON that the Java JSON parser can parse:

{"name": "John", "Age": 24, "Income": null}

Is this data safe for transmission? I think so.

Using the following example:

import json
r = {"name": "Jöhn дое", "Age": 24, "Income": None}
enc = json.JSONEncoder()
print(enc.encode(r))

This is encoded into plain ASCII:

{"name": "J\u00f6hn \u0434\u043e\u0435", "Age": 24, "Income": null}

And if you are not convinced that this is safe for transferring over the wire you can still use base64 encode the output of the JSONEncoder.encode() method.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it advisable to send encoded data using json encoder through the network? Will the data not be corrupted?
@mightyMouse I've added an example with code points above 127. The example shows that these code points are properly escaped. If this is insufficient for your use case you can still base64 encode the output of the JSON encoder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.