I've read so many posts and still can't find or understand how to handle a result set that can have either 1 result, or an array of results. (From yahoo Search)
I can parse the results perfectly fine IF multiple results were received, but when there is only 1 search result I get the JSONException: blahblahbalh is not a JSONArray.
JSONArray results = resultObject.getJSONArray("Result");
Works fine when there are multiple results, but how can I FORCE the built-in JSON parser to accept it as a result when there is only 1 result returned from the Yahoo Query?
This single result fails to parse to JSON Array:
{
"ResultSet": {
"totalResultsAvailable": "108",
"totalResultsReturned": "1",
"firstResultPosition": "1",
"ResultSetMapUrl": "http:\/\/maps.yahoo.com\/broadband\/?q1=Virginia+Beach%2C+VA+23454-4608tt=mexicantp=1",
"Result": {
"id": "12811175",
}
}
}
But this parses to JSONArray just fine:
{
"ResultSet": {
"totalResultsAvailable": "108",
"totalResultsReturned": "2",
"firstResultPosition": "1",
"ResultSetMapUrl": "http:\/\/maps.yahoo.com\/broadband\/?q1=Virginia+Beach%2C+VA+23454-4608tt=mexicantp=1",
"Result": [
{
"id": "12811175",
},
{
"id": "12814560",
}
]
}
}
Sorry if I'm babbling, but it's driving me crazy that I just can't figure out how to get a JSONArray with length of 1, out of the first result example.
Thanks much!
This is one of the problems with working with JSOn. If there are two objects than it is considered an JSONArray, otherwise it is considered a JSONObject.
As far as I know, you need to just assume that it could be either and code accordingly. You could wrap the messy details in a helper function like:
JSONArray getArrayOrObject(JSONObject parent, String name) {
JSONArray results = parent.optJSONArray(name);
if (results == null) {
results = new JSONArray();
JSONObject object = parent.optJSONObject("Result");
if (object != null) {
results.put(object);
}
}
return results;
}
You want to check if totalResultsReturned == 1. if it is, you should use .getJSONObject('Result') instead, and then construct a JSONArray with that object as the first value (http://www.json.org/javadoc/org/json/JSONArray.html).
Presumably you're handling the case where there are 0 results as well.
Related
I have a string like below
{
"id": "abc",
"title": "123.png",
"description": "fruits",
"information": [
{
"type": "apple",
"url": "https://apple.com"
},
{
"type": "orange",
"url": "https://orange.com"
}
],
"versions": 0
}
I want to get the value of url where type: orange. The list in information may not always be in same order as appearing in the data above. I know I could do it easily in python with json.loads and json.dump.
I am trying to do it java using JsonNode and objectMapper.readTree.at("/information") but I am unable to get past this point in a clever neat way to get the list and fetch the url where type = orange.
This is pretty straightforward
Use a JSON library and parse the response using the library. Then get only the values and attributes that you need...
Example relevant to your case:
// Get your Json and transform it into a JSONObject
JSONObject mainObject = new JSONObject(yourJsonString); // Here is your JSON...
// Get your "information" array
JSONArray infoArray = mainObject.getJSONArray("information"); // Here you have the array
// Now you can go through each item of the array till you find the one you need
for(int i = 0 ; i < infoArray.length(); i++)
{
JSONObject item = participantsArray.getJSONObject(i);
final String type = item.getString("type");
final String url = item.getString("url");
if(type.equals("orange"))
{
// DO WHATEVER YOU NEED
}
}
I am trying to do JSON parsing. The JSON data is shown below, I am trying to get the "categories". I was able to JSON parse everything else, but I am not sure what does this "categories" belong to, is it a JSONObject, JSONArray, or something else? I am a newbie and self-taught, usually I am familiar that JSONArray has form of "JSONArray": {["content"]}, and the "content" is JSONObject. But in this case, "categories":["content"]. I am trying to parse this "categories", and turn it to string. Thank you for your help.
{
"results": [
{
"type": "Restaurant",
"id": "jfhuiewjkfkdljiahueijkfnlsdiejkl1484391hjk8421k",
"score": 99.9844207764,
"dist": 15.581982823437135,
"info": "search:ta:840369014527642-US",
"poi": {
"name": "RoofTop Bar",
"categorySet": [
{
"id": 184729472943
}
],
"categories": [
"pub food",
"restaurant"
]}
}]
}
This is what I have tried:
groups = new JSONArray();
groups = response.getJSONArray("results");
if (groups.length() > 0) {
JSONObject resultObject = groups.getJSONObject(0);
if (resultObject.has("poi")) {
if (resultObject.getJSONObject("poi").has("name")) {
nameResult = resultObject.getJSONObject("poi").getString("name");
} else {
nameResult = "Information is not available.";
}
if (resultObject.getJSONObject("poi").has("categories")) {
JSONObject categoriesResult;
categoriesResult = resultObject.getJSONObject("categories").toString();
}
results is an array of objects
The first object contains a property called poi
poi contains a property called categories
So using the top to bottom approach, we can arrive at
const categoriesArray = results[0].poi.categories; // gives categories as an array of strings
const categoriesString = categoriesArray.join(",") // gives categories as string, with comma separated values
I am not sure if it is the actual raw data but the poi object where the categories are contained is malformed. It is missing a closing bracket which could be causing parsing issues.
That apart, the field categories from the poi object is a list of strings I am not sure how you want to format it to a string but you could loop through them and do want you want with them.
In order to obtain them you can access them from your object with results[0].poi.categories or loop through the results before accessing the categories with result.poi.categories where result is the variable containing the currently looped result.
EDIT:
From your code sample, assuming response is a JSONObject you can do the following.
Then to obtain categories in a string without the array format, you can loop through the categories and concatenate them to a string.
String categories = resultObject.get("categories").join(", ");
This question already has answers here:
Merge/Extend JSON Objects using Gson in Java
(4 answers)
Closed 1 year ago.
Given this JSON:
{
"contact_data": [
"address/all",
"telephone/us"
],
"financial_data": [
"bank_account_number/all",
"bank_account_number/uk",
"credit_card_numbers"
]
}
and this JSON:
{
"financial_data": [
"credit_card_numbers",
"bank_account_number/ca",
"bank_account_number/all"
],
"government_id": [
"driving_license/americas"
],
"sensitive_data": [
"racial_ethnic_origin"
]
}
I want to merge these to look like this:
{
"contact_data": [
"address/all",
"telephone/us"
],
"financial_data": [
"credit_card_numbers",
"bank_account_number/ca",
"bank_account_number/uk",
"bank_account_number/all"
],
"government_id": [
"driving_license/americas"
],
"sensitive_data": [
"racial_ethnic_origin"
]
}
I have the following, which almost works:
import org.json.JSONObject;
...
final List<String> jsonStrings = ...; // A list of the above sample JSONs
final List<JSONObject> jsonObjects = jsonStrings
.stream()
.map(JSONObject::new)
// JSONObject.getNames() (called later on) will return null if JSONObject is empty, so filter out empty objects.
.filter(jsonObject -> !jsonObject.isEmpty())
.collect(Collectors.toList());
if (jsonObjects..size() > 1) {
// Merge multiple JSONObjects: https://stackoverflow.com/a/2403453/12177456
final JSONObject firstJsonObject = jsonObjects.get(0);
final JSONObject merged = new JSONObject(firstJsonObject, JSONObject.getNames(firstJsonObject));
final List<JSONObject> remainingJsonObjects = jsonObjects.subList(1, jsonObjects.size());
for (final JSONObject nextJsonObject : remainingJsonObjects) {
for (final String nextJsonObjectFieldName : JSONObject.getNames(nextJsonObject)) {
merged.put(nextJsonObjectFieldName, nextJsonObject.get(nextJsonObjectFieldName));
}
}
return merged;
}
however, where I would expect to see 4 entries in financial_data:
...
"financial_data": [
"bank_account_number/all",
"bank_account_number/uk",
"bank_account_number/ca",
"credit_card_numbers"
]
...
instead, I see just 3, with bank_account_number/uk not in the merged result:
...
"financial_data": [
"bank_account_number/all",
"bank_account_number/ca",
"credit_card_numbers"
]
...
I'm not stuck on using org.json, if it's simplier using gson, jackson, plain Java maps, I'm ok with that.
Actually, it won't work. The problem is here:
merged.put(nextJsonObjectFieldName, nextJsonObject.get(nextJsonObjectFieldName))
This replaces the entry with key nextJsonObjectFieldName with the later one. That is why you're getting in the financial_data from the second object.
{
"financial_data": [
"credit_card_numbers",
"bank_account_number/ca",
"bank_account_number/all"
]
}
You are seeing other keys okay because other keys hase exact same value in both JSON's. If you change the value of other keys too in the second json, you'll see it the merged JSON will have the values with the same key from second json. That is, IT WON'T WORK.
What you can do is, you can check if the key has already value in the map or not. If there's no existing JSONobject for this key, just put it into the merged as you're doing. Otherwise, we have some more job to do:
JSONObject jsonObject = merged.get(nextJsonObjectFieldName);
if (jsonObject != null) {
final JSONObject finalObj = mergeJsonObjectCheckingFieldValues(jsonObject, nextJsonObject.get(nextJsonObjectFieldName));
merged.put(nextJsonObjectFieldName, finalObj);
} else {
merged.put(nextJsonObjectFieldName, nextJsonObject.get(nextJsonObjectFieldName));
}
The mergeJsonObjectCheckingFieldValues method checks each element between the given two JSONObject and compares whether they are same. As per your example and for simply answering this question, I've assumed that each of the JSONObject is nothing but a list of String. For this, we'll be needing objectMapper. So make sure you have the com.fasterxml.jackson.databind.ObjectMapper in your project. So, the checking will be:
ObjectMapper mapper = new ObjectMapper();
public JSONObject mergeJsonObjectCheckingFieldValues(JSONObject jsonObject, JSONObject nextJsonObject)) {
List<String> existingList = Arrays.asList(mapper
.readValue(jsonObject.toString(), String[].class));
List<String> newList = Arrays.asList(mapper
.readValue(nextJsonObject.toString(), String[].class));
List<String> toBeAdded = newList
.stream()
.filter(x -> !existingList.contains(x))
.collect(Collectors.toList());
if(toBeAdded.size() > 0) {
existingList.addAll(toBeAdded);
}
return new JSONObject(JSONArray.toJSONString(existingList));
}
This is the probable solution to your problem. I haven't tested it, but the code should be pretty much this.
I went with the accepted answer here using gson:
https://stackoverflow.com/a/34092374/12177456
Handles recursively merging JsonObjects
Handles conflicts when the key exists in both maps
Has special handling for non-primitive values such as Arrays
I am using JsonPath to retrieve a value from a JSON file. The JSON file looks something like this:
[
{
"username": "John",
"password": {
"passwordValue": "passwordjohn",
"secret_key": "123"
}
},
{
"username": "Nick",
"password": {
"passwordValue": "XXX",
"secret_key": "ZZZ",
"other_key": "YYY"
}
}
]
The JsonPath I am using is to retrieve the password from a particular user. Example:
fun getPassword() {
val passwords: JSONArray = read(jsonFile, "\$.[?(#.name==\"John\")].password")
}
However, I found two obstacles. Firstly, I get back a net.minidev.json.JSONArray always, and the same path with appended [0] doesn't work.
Therefore, I try to get the only element from the JSONArray I get back, like this: credentials[0]. Unfortunately, this removes the double quotes in the field names, resulting in something like this:
{passwordValue: passwordjohn, secret_key: 123}
Which is impossible to work with.
I am looking for a way to get this back:
{"passwordValue": "passwordjohn", "secret_key": "123"}
What I ended up doing was to remove the [ and ] symbols from the beginning of the JSONArray after converting it to a String:
private fun JSONArray.toCredentialString(): String {
val credentialString = this.toString()
return credentialString.substring(1, credentialString.length - 1)
}
Any better solution is welcome.
I have json file in below format.
{
"data":[
{
"prjId": 1,
"name" : "Forj1",
"issue": [
{
"id": 00001,
"status" : "Closed"
},
{
"id": 00002,
"status" : "Open"
}
]
},
{
"prjId": 2,
"name" : "Forj2",
"issue": [
{
"id": 00003,
"status" : "Closed"
},
{
"id": 00004,
"status" : "Open"
}
]
}],
"issueCounter": 7,
"success": true
}
Here "data" is array of projects, and within project attribute there is array of "issue".
So far if I remove "issue" array, I am able to traverse the json to one level down in "data" attribute, If this json has "issue" array I get an error saying missing comma.
javax.json.stream.JsonParsingException: Invalid token=NUMBER at (line no=15, column no=14, offset=242) Expected tokens are: [COMMA]
Below is the code that I have right now. I have two problems with this, one is the error while reading if I place the "issue" attribute, and secondly a way to read the "issue" array and traverse all attributes within.
InputStream fis = new FileInputStream(pathToFile+"data3.json");
JsonReader jsonReader = Json.createReader(fis);
//the error is thrown on below line while reading the above json.
JsonObject jsonObject = jsonReader.readObject();
jsonReader.close();
fis.close();
System.out.println(jsonObject.getInt("issueCounter"));
//reading arrays from json
JsonArray jsonArrayData = jsonObject.getJsonArray("data");
Project [] prj = new Project[jsonArrayData.size()];
int index = 0;
for(JsonValue value : jsonArrayData){
JSONObject jsonObj = new JSONObject(value.toString());
System.out.println(jsonObj.getString("name"));
System.out.println(jsonObj.getInt("prjId"));
//this is also the place where I am stuck, I know I need to construct an array out of it by obtaining issue attribute. Below is very very wrong.
/*
JsonArray jsonArrayIssue = jsonObj.getJsonArray("issue");
for(JsonValue issue : jsonArrayIssue){
JSONObject jsonIssueObj = new JSONObject(issue.toString());
System.out.println(jsonIssueObj.getString("status"));
System.out.println(jsonIssueObj.getInt("id"));
}
*/
}
Any help or pointers is deeply appreciated. I can tweak the json if its required ultimately I need to maintain an array of issues.
The problem as others said is the JSON.
"id": 00001 <-- this is a number, numbers cannot start with a leading zero as per JSON stadard.
If you control the JSON you should tweak it.
Alternatively ff you don't, you can use a less strict parser like org.json.simple https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
The code will be the same as yours, just adjusted to org.json.simple
try { ...
JSONObject rootJSON = (JSONObject) new JSONParser().parse(jsonString);
JSONArray dataList = (JSONArray) rootJSON.get("data");
for(Object projectObj: dataList.toArray()){
JSONObject project = (JSONObject)projectObj;
JSONArray issueList = (JSONArray) project.get("issue");
for(Object issueObj: issueList.toArray()){
JSONObject issue = (JSONObject) issueObj;
//do something with the issue
}
}
} catch (ParseException e) {
//do smth
e.printStackTrace();
}
Your json data is invalid.You can check here.
http://jsonlint.com
...issue": [{ "id": 00001,
"status": ----------------------^
Your id must be string number,string,boolean.Send 1,2,3,.... as return values and check if it works.
Your code looks okay the problem is the JSON formatting. Specifically the following lines:
"id": 00001,
"id": 00002,
"id": 00003,
"id": 00004,
Basically if you want it in that format you will need to set them as strings by wrapping the values in quotations i.e. "id": "00001" or you can use a valid number i.e. "id": 1