1

I have JSON String (see below) and want to parse it into an Java object, that is basically not a problem. But as you can see the type of the value is not fix. Is there a way to parse this into objects with the fields

String key;
<JsonValue> value;
Timestamp time;

or something like this? It is important to me, to obtain the type of the value in a way that i can persist the data into a database.

[
    {
      "key": "someKey",
      "value": "SomeValue",
      "time": "2016-03-30 14:59:55.108"
    },
    {
      "key": "otherKey",
      "value": 42,
      "time": "2016-03-30 14:59:55.108"
    }
]
4
  • Why not parse your value for value as Object? Commented Mar 30, 2016 at 13:06
  • you mean a field of type Object? Like Object value;? Commented Mar 30, 2016 at 13:07
  • yep that's what I mean. Commented Mar 30, 2016 at 13:10
  • Take a look at Jackson. It's a terrific Java library for JSON. Commented Mar 30, 2016 at 13:11

3 Answers 3

2
String key;
Object value;
Timestamp time;

Then you can use instanceof (ex. value instanceof String) to identify the type.

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

5 Comments

Well I thought about this solution, but I want to persist this value into a postgres database. And do not want to lose the type information the JSON value contained at the beginning...
Uhm, how is defined the field in the table?
It is not fixed yet, but i thought about using a column of type json
Wait, so you want to save all the response in a field (and that's pratically for free) plus you need a POJO to do some data manipulation? Why do you need to keep the type if in the db you're gonna store the whole JSON message? You can always add a String in the POJO to store the whole JSON...
oh now i do not want to store the whole message... I was going to have a table like this: keyColumn:text; valueColumn:Json; timeColumn:timestamp.
0

Why not use generic type?

class JsonObj {
    String key; E value; Timestamp time;
}

And create concrete object based on your JSON.

Comments

0

What you’re trying to achieve seems to me a contradiction of basic JSON schema principles or schema validation in general.

If you were to define a schema representing your JSON documents, you would probably struggle to produce one due to ambiguity of the type corresponding to the “value” property.

If you want to preserve the ‘type’ of the value, I would suggest including it into your JSON document, e.g.

[
{
      "key": "someKey",
      "value": "3.123",
      "type": "number",
      "time": "2016-03-30 14:59:55.108"
},
{
      "key": "someKey",
      "value": "someValue",
      "type": "string",
      "time": "2016-03-30 14:59:55.108"
}
]

The corresponding JSON schema would be:

{
  "$schema": "http://json-schema.org/draft-04/schema#",  
  "description": "Some description",
  "type" : "array",
  "element" : {
      "key" : {
        "type" : "string"
      },
      "value" : {
        "type" : "string"
      },
      "type" : {
        "type" : "string"
      },      
      "time" : {
        "type" : "string"
      }
  },
  "required": ["key", "value", "type", "time"]
}

Note your database column also needs to be of a specific type hence string equivalent type for storing multiple data types is probably the best fit.

Comments

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.