Read JSONObject by using java8 stream - json

Here I have to get matching key and value pair, if it is available in array I have to print whole array without creating the object.
This what I'm expecting,
Input :
{
"key": [
{
"k1": "k1v1",
"k2": "k2v1",
"k3": "k3v1",
"k4": "k4v1"
},
{
"k1": "k1v2",
"k2": "k2v2",
"k3": "k3v1",
"k4": "k4v2"
},
{
"k1": "k1v1",
"k2": "k2v2",
"k3": "k3v3",
"k4": "k4v4"
}
]
output : search value is : k1v1/k1 (if we are pass key or value the output should be like this)
{"k1": "k1v1","k2": "k2v1","k3": "k3v1","k4": "k4v1"},{"k1": "k1v1","k2": "k2v2","k3": "k3v3","k4": "k4v4"}
I tried this code
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("keys.json"));
JSONArray keysArray = (JSONArray) jsonObject.get("key");
String jsonStr = JSONArray.toJSONString(keysArray);
List<Object> key = ((Stream<JSONObject>)(keysArray.stream()))
.map(key1 -> (JSONObject)(key1))
.map(key1 -> (key1.get(str1)))
.filter(key1 -> key1.equals(str1))
.collect(Collectors.toList());
This what I'm getting the output,
key["k1v1","k1v1"]

Related

how can I exclude a key value pair if jsonArray is sending empty array?

jsonArray.put(jsonObject);
if (!jsonArray.isEmpty()) {
shopProductJsonObject.put("productBasedOffer", jsonArray);
shopProductListJsonArray.put(shopProductJsonObject);
}
"productBasedOffer": [
{
"offerKeyword": "null",
"minAmount": "0.0",
"offerType": "PRODUCT_BASED",
"amount": "50.0",
"offerId": "27",
"freeProductId": "0",
"endTime": "1613757599000",
"offerCategory": "FIXED"
}
]
"productBasedOffer": []
If jsonArray is sending empty array, I want to skip that key value pair.
how can I implement this? I'm using spring boot.
Get the JSONArray :
JSONObject jsonObject = new JSONObject(yourString); // yourString is String type
JSONArray arr = jsonObject.getJSONArray("productBasedOffer");
if(arr!=null && arr.length()>0)
{
//Do something
}
If you write your logic in if() block, then of course it'll skip the current one.

Unexpected character (g) at position 0 when trying to parse json - HttpResponseDecorator

Whenever I try to execute below script I keep getting error, saying unexpected character (g).
Basically I want to be able to parse the json response and be able to get the upstream job name from it.
Script:
#Grapes([
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1'),
#Grab(group='commons-collections', module='commons-collections', version='3.2.1'),
#Grab(group='org.jsoup', module='jsoup', version='1.10.2'),
#Grab(group='org.json', module='json', version='20190722'),
#Grab(group='com.googlecode.json-simple', module='json-simple', version='1.1.1')
])
import static groovyx.net.http.ContentType.*
import groovyx.net.http.HttpResponseException
import groovyx.net.http.RESTClient
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import java.net.*
import java.util.*
import org.json.simple.*
import org.json.simple.parser.JSONParser;
def getRestClient(){
String jenkinsUrl="http://somedomainname:8080"
def restClient = new RESTClient(jenkinsUrl)
return restClient
}
def getJobsInfo(String jobname,RESTClient restClient){
def requrl= '/job/'+jobname+'/lastBuild/api/json/?pretty=true'
def response = restClient.get( path : requrl)
return response
}
def writeToPropertyFile(){
def jsonResponseObject = getJobsInfo("sampleJobName",getRestClient())
println "\n\n\n\n\n Json String :---- "+ jsonResponseObject.toString()
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonResponseObject.toString());
JSONArray upstreamJobInfoArray = jsonObject.getJSONArray("causes");
for (int i = 0; i < upstreamJobInfoArray.length(); i++) {
JSONObject jobCauses = upstreamJobInfoArray.getJSONObject(i);
String upstreamProjectName = jobCauses.getString("upstreamProject");
println upstreamProjectName
}
}
writeToPropertyFile()
Error :
Json String :---- groovyx.net.http.HttpResponseDecorator#6b063470
Caught: Unexpected character (g) at position 0.
Unexpected character (g) at position 0.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at org.json.simple.parser.JSONParser$parse.call(Unknown Source)
at getUpstreamJob.writeToPropertyFile(getUpstreamJob.groovy:39)
at getUpstreamJob.run(getUpstreamJob.groovy:50)
EDIT 1 : START
JSON response that I am trying to parse :
{
"_class": "hudson.model.FreeStyleBuild",
"actions": [
{
"_class": "hudson.model.CauseAction",
"causes": [
{
"_class": "hudson.model.Cause$UpstreamCause",
"shortDescription": "Started by upstream project \"sampleJobName\" build number 712",
"upstreamBuild": 712,
"upstreamProject": "sampleJobName",
"upstreamUrl": "job/sampleJobName/"
},
{
"_class": "hudson.model.Cause$UserIdCause",
"shortDescription": "Started by user Malick, Asif",
"userId": "asifma00",
"userName": "Malick, Asif"
},
{
"_class": "com.sonyericsson.rebuild.RebuildCause",
"shortDescription": "Rebuilds build #300",
"upstreamBuild": 300,
"upstreamProject": "sampleJobName",
"upstreamUrl": "view/ABCProjectView/job/sampleJobName/"
}
]
},
{
"_class": "hudson.model.ParametersAction",
"parameters": [
{
"_class": "hudson.model.StringParameterValue",
"name": "SNAPSHOTNAME",
"value": "ABCDE_12121.2000-2121212121212"
},
{
"_class": "hudson.model.StringParameterValue",
"name": "BUILD_LABEL",
"value": "ABCDE_12121.2000"
}
]
},
{},
{},
{},
{},
{
"_class": "hudson.plugins.parameterizedtrigger.BuildInfoExporterAction"
},
{},
{},
{},
{}
],
"artifacts": [],
"building": false,
"description": null,
"displayName": "#301",
"duration": 1199238,
"estimatedDuration": 1194905,
"executor": null,
"fullDisplayName": "sampleJobName #301",
"id": "301",
"keepLog": false,
"number": 301,
"queueId": 189076,
"result": "SUCCESS",
"timestamp": 1583500786857,
"url": "http://somedomainname:8080/job/sampleJobName/301/",
"builtOn": "Server12345",
"changeSet": {
"_class": "hudson.scm.EmptyChangeLogSet",
"items": [],
"kind": null
},
"culprits": []
}
EDIT 1 : END
I have tried looking at several Stack Overflow issues, but still haven't been able to resolve it.
Please guide.
It's because you're not getting the actual JSON string, you're getting the toString() output on a groovyx.net.http.HttpResponseDecorator class.
You can see it printing it out here (it's easy to miss though... I missed it the first look 🙂):
Json String :---- groovyx.net.http.HttpResponseDecorator#6b063470
I think you need to call jsonResponseObject.data to get the parsed data, and it might even return you a Map parsed form the JSON (so you can get rid of all your JSONObject code). Test it by changing to:
def data = jsonResponseObject.data
println "\n\n\n\n\n Json ${data.getClass().name} :---- $data" jsonResponseObject.toString()
If it is a java.util.Map, you can simplify your second part from:
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonResponseObject.toString());
JSONArray upstreamJobInfoArray = jsonObject.getJSONArray("causes");
for (int i = 0; i < upstreamJobInfoArray.length(); i++) {
JSONObject jobCauses = upstreamJobInfoArray.getJSONObject(i);
String upstreamProjectName = jobCauses.getString("upstreamProject");
println upstreamProjectName
}
To (using data from above):
data.actions.causes.upstreamProject.flatten().each { println it }

How do I get "keys" from an unkown .json saved in my computer using groovy

My end goal is to parse thru an unknown .json file stored on my laptop and get the key names (not the values only key names) using Groovy in SoapUI.
I want to parse an unknown JSON file stored in my computer and get its keys (name of the keys, not the values). I can do 2 things separately:
I am able to read the local JSON using the following code I found online:
def JSON_URL = "file:///C:/Users/xxx/example.json"
URL url = new URL(JSON_URL)
InputStream urlStream = null
try {
urlStream = url.openStream()
BufferedReader reader = new BufferedReader(new Inpu tStreamReader(urlStream))
JsonSlurper js1 = new JsonSlurper()
Object result = js1.parse(reader)
log.info "==> readJSONfile2 result of read: "+result
} catch (Exception e) {
log.info e
}
And if I have the URL, I am able to parse it and get keys like so:
// getting the response
def resp1 = context.expand('${testStepName#response}')
// parsing the set context
def js1 = new JsonSlurper().parseText(resp1)
def keys = js1.entrySet() as List
log.info "==> runTreatmentPlan keys list is: "+keys
log.info "==> runTreatmentPlan keys size is: "+keys.size()
But I am unable to get the keys if the JSON is local to my machine ie unable to combine both the codes. I get error when I do:
Object result = js1.parseText(reader)
I am new to groovy and SoapUI and json - a total newbie and this is my first question. I am really scared because I have seen that some people are kinda rough towards other newbies if the question is basic. I promise, I did google a lot, and I am sure the experienced people might find my question stupid as well, but I am really stuck. I am unable to combine both pieces of code. I kinda feel that I will some how have to use the response from #1 code, but I don't know how.
Can someone help me please?
==== Updating with the JSON structure:
{
"Key0": [
{
"Key1": "Value1",
"Key2": "Value2",
"Key3": "Value3",
"Key4": {
"subKey1": "subValue1"
},
"Key5": {
"subKey1": "subValue1",
"subKey2": "subValue2",
"subKey3": "subValue3",
"subKey4": "subValue4"
},
"Key6": "2016-07-11T17:52:59.000Z",
"Key7": [
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
}
]
},
{
"Key1": "Value1",
"Key2": "Value2",
"Key3": "Value3",
"Key4": {
"subKey1": "subValue1"
},
"Key5": {
"subKey1": "subValue1",
"subKey2": "subValue2",
"subKey3": "subValue3",
"subKey4": "subValue4"
},
"Key6": "2016-07-11T17:52:59.000Z",
"Key7": [
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
},
{
"subKey1": "subValue1",
"subKey2": "subValue2"
}
]
}
]
}
Given your input, this code:
import groovy.json.JsonSlurper
def traverse
traverse = { tree, keys = [], prefix = '' ->
switch (tree) {
case Map:
tree.each { k, v ->
def name = prefix ? "${prefix}.${k}" : k
keys << name
traverse(v, keys, name)
}
return keys
case Collection:
tree.eachWithIndex { e, i -> traverse(e, keys, "${prefix}[$i]") }
return keys
default :
return keys
}
}
def content = new JsonSlurper().parse( new File( 'sample.json' ) )
traverse(content).each { println it }
Yields this output:
Key0
Key0[0].Key1
Key0[0].Key2
Key0[0].Key3
Key0[0].Key4
Key0[0].Key4.subKey1
Key0[0].Key5
Key0[0].Key5.subKey1
Key0[0].Key5.subKey2
Key0[0].Key5.subKey3
Key0[0].Key5.subKey4
Key0[0].Key6
Key0[0].Key7
Key0[0].Key7[0].subKey1
Key0[0].Key7[0].subKey2
Key0[0].Key7[1].subKey1
Key0[0].Key7[1].subKey2
Key0[0].Key7[2].subKey1
Key0[0].Key7[2].subKey2
Key0[0].Key7[3].subKey1
Key0[0].Key7[3].subKey2
Key0[1].Key1
Key0[1].Key2
Key0[1].Key3
Key0[1].Key4
Key0[1].Key4.subKey1
Key0[1].Key5
Key0[1].Key5.subKey1
Key0[1].Key5.subKey2
Key0[1].Key5.subKey3
Key0[1].Key5.subKey4
Key0[1].Key6
Key0[1].Key7
Key0[1].Key7[0].subKey1
Key0[1].Key7[0].subKey2
Key0[1].Key7[1].subKey1
Key0[1].Key7[1].subKey2
Key0[1].Key7[2].subKey1
Key0[1].Key7[2].subKey2
Key0[1].Key7[3].subKey1
Key0[1].Key7[3].subKey2
import groovy.json.JsonSlurper
/**
* sample.json present in C:/tools/
* { "foo": "bar", "baz": 123 }
*/
def content = new JsonSlurper().parse( new File( 'C:/tools/sample.json' ) )
assert content.keySet() == ['baz', 'foo'] as Set
UPDATE:
After looking at the actual json structure and the issues with the version of Groovy in SOAPUI, below would be a viable option to get all the keys in a specific format:
import groovy.json.JsonSlurper
def textFromFile = new File('/path/to/example.json').text
def content = new JsonSlurper().parseText(textFromFile)
def getAllKeys(Map item) {
item.collect { k, v ->
v instanceof Map ? convertToString(v).collect { "${k}.$it" } : k
}.flatten()
}
assert getAllKeys(content) == [
'Key1', 'Key2', 'Key3',
'Key4.subKey1', 'Key5.subKey1', 'Key5.subKey2',
'Key5.subKey3', 'Key5.subKey4',
'Key6', 'Key7'
]

extract case classes from json file scala play

im trying to extract my data from json into a case class without success.
the Json file:
[
{
"name": "bb",
"loc": "sss",
"elements": [
{
"name": "name1",
"loc": "firstHere",
"elements": []
}
]
},
{
"name": "ca",
"loc": "sss",
"elements": []
}
]
my code :
case class ElementContainer(name : String, location : String,elements : Seq[ElementContainer])
object elementsFormatter {
implicit val elementFormatter = Json.format[ElementContainer]
}
object Applicationss extends App {
val el = new ElementContainer("name1", "firstHere", Seq.empty)
val el1Cont = new ElementContainer("bb","sss", Seq(el))
val source:String=Source.fromFile("src/bin/elementsTree.json").getLines.mkString
val jsonFormat = Json.parse(source)
val r1= Json.fromJson[ElementContainer](jsonFormat)
}
after running this im getting inside r1:
JsError(List((/elements,List(ValidationError(List(error.path.missing),WrappedArray()))), (/name,List(ValidationError(List(error.path.missing),WrappedArray()))), (/location,List(ValidationError(List(error.path.missing),WrappedArray())))))
been trying to extract this data forever, please advise
You have location instead loc and, you'll need to parse file into a Seq[ElementContainer], since it's an array, not a single ElementContainer:
Json.fromJson[Seq[ElementContainer]](jsonFormat)
Also, you have the validate method that will return you either errors or parsed json object..

How to create a JSON structure for this

Can you tell me how to create a Json objecct for this
I think it is a JSON with children JSON objects.
[{
"data": {
"ID": "56e819a90111257894be41c9e310f8cb",
"name": "Email_____2016-03-15 10_18_16",
"objCode": "DOCU",
"description": null,
"lastUpdateDate": "2016-03-15T10:18:17:551-0400"
}
}: null]
I did this
JSONObject childVal = new JSONObject();
JSONObject parent = new JSONObject();
try {
childVal.put("ID", "56e819a90111257894be41c9e310f8cb");
childVal.put("name", "Email_____2016-03-15 10_18_16");
childVal.put("objCode", "DOCU");
childVal.put("description", "null");
childVal.put("lastUpdateDate", "2016-03-15T10:18:17:551-0400");
parent.put("data", childVal);
but how do i get the parent [ object : null]
thanks