Parse JSON and convert object back to JSON - json

I want to parse a nested JSON structure in Groovy. I would like to parse a sub element structure and then return the string in JSON format.
The Nested JSON structure:
{
"username": "test",
"token": "test1",
"url": "http://www.abc.to",
"testsession":
{
"serverName": "0.0.0.0",
"serverPort": 22,
"remoteUsername": "admin",
"remotePassword": "admin"
},
"deviceapp":
{
"repo": "abc-mvn-a-test-local",
"path": "com/test\/test2\/test3\/mob",
"platform": "ANDROID"
}
}
my code below using JSONSlurper isn't quite giving me what i want:
def slurper = new JsonSlurper().parseText(json)
String deviceAppParsed = slurper.deviceapp
println "deviceAppParsed " + deviceAppParsed
// returns deviceAppParsed {repo=oxp-mvn-a-rel-local, path=com/nagra/opentv/experience/mob, platform=ANDROID}
def jsonDeviceApp = JsonOutput.toJson(deviceAppParsed)
println "IS IT JSON? " + jsonDeviceApp
// returns IS IT JSON "{repo=oxp-mvn-a-rel-local, path=com/nagra/opentv/experience/mob, platform=ANDROID}"
How can i parse the json to retrieve the nested deviceapp structure in raw JSON? Thanks.
:
def slurper = new JsonSlurper().parseText(json)
String deviceAppParsed = slurper.deviceapp
def jsonDeviceApp = JsonOutput.toJson(deviceAppParsed)
I expected println jsonDeviceApp to return:
{"repo": "abc-mvn-a-test-local","path": "com/test\/test2\/test3\/mob","platform": "ANDROID"}
instead it returned:
"{repo=oxp-mvn-a-rel-local, path=com/nagra/opentv/experience/mob, platform=ANDROID}"

just replace String to def in the following line:
String deviceAppParsed = slurper.deviceapp
by using string you are converting Object returned by slurper.deviceapp to string
should be:
def deviceAppParsed = slurper.deviceapp
in this case last line will print json
{"repo":"abc-mvn-a-test-local","path":"com/test/test2/test3/mob","platform":"ANDROID"}

Related

Fetch data with getJSONArray in kotlin

I'm trying to fetch some data from json. I made function to fetch JSON.
My code:
val obj = JSONObject(getJSONFromAssets()!!)
val userArray = obj.getJSONObject("user_id")
My code to fetchAssets:
private fun getJSONFromAssets(): String ?{
var json: String? = null
val charset: Charset = Charsets.UTF_8
try{
val jsonFile = assets.open(K.json.jsonFileName)
val size = jsonFile.available()
val buffer = ByteArray(size)
jsonFile.read(buffer)
jsonFile.close()
json = String(buffer, charset)
}
catch (ex: IOException){
ex.printStackTrace()
return null
}
return json
}
JSON File:
{
"user_id": "1",
"acounts": [
{
"id": "1",
"AccountNumber": "HR123456789012345678901",
"amount": "2.523,00",
"currency": "USD",
"transactions":
UserModelClass:
data class UserModelClass (
val user_id:String,
val accounts: List<AccountsList>
)
Problem is that I have user ID and then accounts where I can loop easy.
Error: Failed to open file, no such file or directory.
Value 1 at user_id of type java.lang.String cannot be converted to JSONObject

Rest Assured - compare json response with local json file

I have a local json file test.json
[
{
"id": 1,
"title": "test1"
},
{
"id": 2,
"title": "test2"
}
]
Class to read the json file
public static String getFileContent(String fileName){
String fileContent = "";
String filePath = "filePath";
try {
fileContent = new String(Files.readAllBytes(Paths.get(filePath)));
return fileContent;
}catch(Exception ex){
ex.printStackTrace();
}finally{
return fileContent;
}
}
I use rest assured to make the request and get same json response back
String fileContent= FileUtils.getFileContent("test.json");
when().
get("/testurl").
then().
body("", equalTo(fileContent));
This is what I got from local file
[\r\n {\r\n \"id\": 1,\r\n \"title\": \"test1\"\r\n },\r\n {\r\n \"id\": 2,\r\n \"title\": \"test2\"\r\n }\r\n]
This is the actual response:
[{id=1, title=test1}, {id=2, title=test2}]
Is there any better way to compare those two? I try to do something like fileContent.replaceAll("\\r\\n| |\"", ""); but it just removed all the space [{id:1,title:test1},{id:2,title:test2}]
Any help? Or any ways that just compare the content and ignore newline, space and double quote?
You can use any of the following methods
JsonPath :
String fileContent = FileUtils.getFileContent("test.json");
JsonPath expectedJson = new JsonPath(fileContent);
given().when().get("/testurl").then().body("", equalTo(expectedJson.getList("")));
Jackson :
String fileContent = FileUtils.getFileContent("test.json");
String def = given().when().get("/testurl").then().extract().asString();
ObjectMapper mapper = new ObjectMapper();
JsonNode expected = mapper.readTree(fileContent);
JsonNode actual = mapper.readTree(def);
Assert.assertEquals(actual,expected);
GSON :
String fileContent = FileUtils.getFileContent("test.json");
String def = given().when().get("/testurl").then().extract().asString();
JsonParser parser = new JsonParser();
JsonElement expected = parser.parse(fileContent);
JsonElement actual = parser.parse(def);
Assert.assertEquals(actual,expected);

getting an error groovy.lang.MissingPropertyException: No such property even when values are present

Values are present in response, can print them via log.info but giving me an error when adding them in array, here is my groovy script,
import groovy.json.*
def ResponseMessage = ''' {
"Unit": {
"Profile": 12,
"Name": "Geeta"
},
"UnitID": 2
} '''
def json = new JsonSlurper().parseText(ResponseMessage)
log.info json.UnitID
log.info json.Unit.Profile
log.info json.Unit.Name
def arrayjson = json.collectMany { s ->
[s.UnitID,s.Unit.Profile,s.Unit.Name]
}
log.info "arrayjson : " + arrayjson
And The error message ,
groovy.lang.MissingPropertyException: No such property: UnitID for class: java.util.HashMap$Entry Possible solutions: key error at line: 14
The collectMany iterates over key/value pairs. Consider the following (as far as I understand the goal):
import groovy.json.*
def ResponseMessage = ''' {
"Unit": {
"Profile": 12,
"Name": "Geeta"
},
"UnitID": 2
} '''
def json = new JsonSlurper().parseText(ResponseMessage)
println json.UnitID
println json.Unit.Profile
println json.Unit.Name
// this illustrates how collectMany works, though it does
// not solve the original goal
json.collectMany { key, val ->
println "key: ${key} , val: ${val}"
[]
}
def arrayjson = [json.UnitID,json.Unit.Profile,json.Unit.Name]
println "arrayjson : " + arrayjson

GSON - json to generic object

I have a use case where an API will get a generic collection of Key|Value pairs in json. There are no defined attributes in the input json. I need to map it to a generic object and process the data..
JSON input:
"[{ "PostalCode": "345", "Region": "MA", "Enabled": "True" },
{"PostalCode": "989", "Country": "US", "Enabled": "True" }
]";
I am using GSON to deserialize this to java object. On mapping this to a generic object like:
Object obj = new GsonBuilder().create()
.fromJson(jsonInput, Object.class);
i get a an object of Array list of HashMaps (com.google.gson.internal.LinkedTreeMap).
From here how do i get individual key and values like key = PostalCode & value = 345?
Thanks in advance for your help!
As you have already got an array list of com.google.gson.internal.LinkedTreeMap, Now you need to iterate this list to get each key value pair :
Object obj = new GsonBuilder().create().fromJson(jsonInput, Object.class);
List<LinkedTreeMap<Object, Object>> jsonMapList = (List<LinkedTreeMap<Object, Object>>) obj;
for (LinkedTreeMap<Object, Object> jsonMap : jsonMapList) {
Set<Entry<Object, Object>> entrySet = jsonMap.entrySet();
for (Entry<Object, Object> entry : entrySet) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
You can do this way. I have tested it.
import this
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
Type type = new TypeToken<List<LinkedTreeMap<Object, Object>>>(){}.getType();
List<Map<Object, Object>> treeMap = newGsonBuilder().create().fromJson(jsonInput,type);

Groovy - Constructing json from String

I'm using Groovy, i've tried to create a simple function which will construct a Json object from a provided Json string, then i'm trying to print this string but unfortunate it's adding Square brackets to the output.
Here's a snippet from my code:
def JsonBuilder ConstructJsonObject (jsonStr) {
def jsonToReturn = new JsonBuilder();
def root = jsonToReturn(jsonStr);
return jsonToReturn;
}
String jsonStr = "{id: '111'}";
println(jsonStr);
def jsonObject = ConstructJsonObject(jsonStr);
println(jsonObject.toPrettyString());
And here's the output:
{id: '111'}
[
"{id: '111'}"
]
It's returning an Array and not a pure Json.
If you change your input to be valid json (with double quotes round the keys and values), you can do:
import groovy.json.*
String jsonStr = '{"id": "111"}'
println new JsonBuilder(new JsonSlurper().parseText(jsonStr)).toPrettyString()
To print
{
"id": "111"
}