Retrieve single field from json string field

I have a string field with the following data:
{"vin":"VIN4","brand":"Merk4","type":"Type4","year":2004}

How can I retrieve the value for the fields vin, brand, type and year and print them in my document?

My string never contains repeating data.

I cannot use query to retrieve the data from the json string (I use Servoy foundset).

karin_1's picture
Joined: Feb 22 2017 - 4:45am
Last seen: 5 months 3 weeks ago

Thanks for you reply. It works perfectly. And I managed to make the code even more disgusting :-)
 

$V{Variable_1}.contains("vin")?
$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).substring(0,$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).indexOf("\",\"")):"no VIN specified"

If the vin field doest not exist, it will not show wrong info. 

karin_1 - 1 year 7 months ago

1 Answer:

Here is some red hot garbage that might do the trick:

$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).substring(0,$V{Variable_1}.substring($V{Variable_1}.indexOf("\"vin\":") + new String("\"vin\":").length() + 1).indexOf("\",\""))

This should return the value for "vin" for data stored in $V{Variable_1}.

Yes. I know. This is disgusting. ;-)

Update 2022: I'm stupid and this is the proper way to do it:

new org.json.JSONObject($V{JSON}).get("vin").toString()

zellers's picture
653
Joined: Feb 12 2019 - 11:18am
Last seen: 3 weeks 2 days ago
Feedback