This question already has answers here:
Getting a single value from a JSON object using JSONPath
(4 answers)
Closed 3 years ago.
I have following JSON
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
and $.phoneNumbers[:1].type json path to extract phone number type iPhone value but the output is
[
"iPhone"
]
I would like to get it as simple string like iPhone. How do i create the JSON path to strip double quote " and [ ?
You cannot remove [ & " directly from JSON Path, However, you can use this replace function given here https://community.apigee.com/questions/63281/how-to-remove-square-bracket-and-double-quotes-fro.html
var extracted_json = ["vIMS"]; // use context.getVariable("<<extracted-variable>>");
modifiedJson = JSON.stringify(extracted_json).replace(/[\[\]"]+/g,"");
context.setVariable("final-variable", modifiedJson)
By default, using JsonPath to process JSON means you will get the result as a JSONArray. So, you have to make one more step to get the desired result.
For example,
#Test
public void test() {
String json = "{\n" +
" \"firstName\": \"John\",\n" +
" \"lastName\" : \"doe\",\n" +
" \"age\" : 26,\n" +
" \"address\" : {\n" +
" \"streetAddress\": \"naist street\",\n" +
" \"city\" : \"Nara\",\n" +
" \"postalCode\" : \"630-0192\"\n" +
" },\n" +
" \"phoneNumbers\": [\n" +
" {\n" +
" \"type\" : \"iPhone\",\n" +
" \"number\": \"0123-4567-8888\"\n" +
" },\n" +
" {\n" +
" \"type\" : \"home\",\n" +
" \"number\": \"0123-4567-8910\"\n" +
" }\n" +
" ]\n" +
"}";
net.minidev.json.JSONArray iPhone = JsonPath.parse(json).read("$.phoneNumbers[?(#.type=='iPhone')].type");
System.out.println("Type : " + iPhone.get(0).toString());
}
, will eventually print Type : iPhone
Related
I am using the following code to read the CSV file and parse its data to JSON.
File inputFile = new File("in.csv");
File outputFile = new File("out.json");
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
CsvMapper csvMapper = new CsvMapper();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.writerWithDefaultPrettyPrinter().writeValue(outputFile,
csvMapper.readerFor(Map.class).with(csvSchema).readValues(inputFile).readAll());
This is working fine and giving me output as follow,
[
{
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
}, {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
]
But the required output is
{
{
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
}, {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
}
Actually I need to read the JSONs files that I have converted from CSVs. Using the following code
String content = Files.readString(filePath);
JSONObject jsonObject1 = new JSONObject(content);
HashMap yourHashMap1 = new Gson().fromJson(jsonObject1.toString(), HashMap.class);
But while I am trying to do it gives me this error.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
Meanwhile json file should start with { instead of [ and similarly it should end on } instead ].
My goal is to remove this error.
I think the result you are seeing is correct and that is the way JSON is supposed to be. JSON is a key value based format.
The output which you have pasted just means it is an array of json objects
[
{
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
}, {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
]
The below JSON actually makes no sense, as there are no keys to the object. Even if you try to parse this kind of JSON with Jackson, it will throw an error Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('{' (code 123)): was expecting double-quote to start field name
at [Source: (String)"{ . You can try this out
{
{
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
}, {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
}
The other option would be to consider each json object as a unique node with a distinct name like below, see the keyword set1 and set2
{
"set1": {
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
},
"set2": {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
}
For some reason if you really want to have {} instead of [] then just make a string operation and replace 1st "[" with "{" and last "]" with "}
Edited the answer to match the edited question:
Now as we know your JSON is an array of JSON objects, you have to read it as JSONArray instead of JSONObject, also you can no more read it to a hashmap, it has to be a list, where each element in the list will be a JSONObject which has your data. Working code snippet is below
String content = Files.readString(filePath);
JSONArray jsonArray = new JSONArray(content);
List<LinkedTreeMap> yourList = new Gson().fromJson(jsonArray.toString(), ArrayList.class);
for(LinkedTreeMap l : yourList) {
System.out.println(l.get("Amount"));
System.out.println(l.get("Nutrient"));
System.out.println(l.get("Unit"));
}
Below is the pojo holding data
class user{
private string userId;
private string role;
private string accessCode;
}
From DB I am getting the below response as one user can have multiple accessCode
"data": [
{
"userId": "userId1",
"role": "admin",
"accesscode": "000008"
},
{
"userId": "userId1",
"role": "admin",
"accesscode": "000007"
},
{
"userId": "userId2",
"role": "analyst",
"accesscode": "000001"
}
]
Expected output as Final Json from rest endpoint
"data": [
{
"userId": "userId1",
"role": "admin",
"accesscode": "000008","000007" // group all access code.
}
{
"userId": "userId2",
"role": "analyst",
"accesscode": "000001"
}
]
What is the best way to achieve this. Any pointers
Here is one way to do it, without using POJOs:
String input = "{" +
" \"data\": [\n" +
" {\n" +
" \"userId\": \"userId1\",\n" +
" \"role\": \"admin\",\n" +
" \"accesscode\": \"000008\"\n" +
" },\n" +
" {\n" +
" \"userId\": \"userId1\",\n" +
" \"role\": \"admin\",\n" +
" \"accesscode\": \"000007\"\n" +
" },\n" +
" {\n" +
" \"userId\": \"userId2\",\n" +
" \"role\": \"analyst\",\n" +
" \"accesscode\": \"000001\"\n" +
" }\n" +
" ]\n" +
"}";
ObjectMapper mapper = new ObjectMapper();
Map<String, List<Map<String, String>>> data = mapper.readValue(input,
new TypeReference<Map<String, List<Map<String, String>>>>() {/**/});
Map<String, List<Map<String, Object>>> combinedData = new HashMap<>();
combinedData.put("data", data.get("data").stream()
.collect(Collectors.groupingBy(
u -> Arrays.asList(u.get("userId"), u.get("role")),
Collectors.mapping(u -> u.get("accesscode"), Collectors.toList())))
.entrySet().stream()
.map(e -> {
Map<String, Object> user = new LinkedHashMap<>();
user.put("userId", e.getKey().get(0));
user.put("role", e.getKey().get(1));
user.put("accesscode", e.getValue());
return user;
})
.collect(Collectors.toList()));
String output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(combinedData);
System.out.println(output);
Output
{
"data" : [ {
"userId" : "userId2",
"role" : "analyst",
"accesscode" : [ "000001" ]
}, {
"userId" : "userId1",
"role" : "admin",
"accesscode" : [ "000008", "000007" ]
} ]
}
Please guys what code should I write on my .java file in android studio so I can be able to ACCESS and DISPLAY strings from the .JSON format below.
{
"question" : [
{
"id" : "0",
" question " : " ",
" options " :
{
"A" : " ",
"B" : " ",
"C" : " ",
"D" : " ",
} ,
" answer" : " "
} ,
My current json readable format in file is .
{"delete" : {
"_index" : "production",
"_type" : "listings",
"_id" : "f170321064",
"_version" : 6,
"result" : "deleted",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 175987,
"_primary_term" : 1,
"status" : 200
}
}
How to convert like below
{"delete" : {"_index" : "production-cire","_type" : "listings","_id" : "424-l-81694-f1703210641700147","_version" : 6,"result" : "deleted","_shards" : {"total" : 1,"successful" : 1,"failed" : 0},"_seq_no" : 175987,"_primary_term" : 1,"status" : 200}}
Not sure what language you are using but I think this will work for you.
let str = JSON.stringify({you_json})
if using java or android:
The string exactly as you wrote it, I'm not sure you can turn it into JSON. but if you change it a bit like what follows it gives what you want:
String str="{" +
"delete : { " +
"_index : production, " +
"_type : listings, " +
"_id : f170321064, " +
"_version : 6, " +
"result : deleted, " +
"_shards : { " +
"total : 1, " +
"successful : 1, " +
"failed : 0" +
"}, " +
"_seq_no : 175987, " +
"_primary_term : 1, " +
"status : 200" +
"}" +
"}";
JSONObject obj= null;
try {
obj = new JSONObject(str);
System.out.println(obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
see the output:
{"delete":{"_index":"production","_type":"listings","_id":"f170321064","_version":6,"result":"deleted","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":175987,"_primary_term":1,"status":200}}
I'm fairly familiar with creating Avro pair schemas in Java. For example,
static final String RECORD_STRING =
"{\n" +
" \"type\": \"record\",\n" +
" \"name\": \"exampleRecord1\",\n" +
" \"fields\": [\n" +
" {\"name\": \"id\", \"type\": \"string\"}\n" +
"]\n" +
"}";
static{
final Schema.Parser parser = new Schema.Parser();
Schema KEY_SCHEMA = parser.parse(RECORD_STRING);
Schema VALUE_SCHEMA = Schema.create(Schema.Type(STRING)) ;
Schema PAIR_SCHEMA = Pair.getPairSchema(KEY_SCHEMA, VALUE_SCHEMA);
}
How would I go about writing this Pair Schema directly as JSON in a file without using any Java?
The Schema class has a toString() method that does what you want:
import org.apache.avro.Schema;
import org.apache.avro.mapred.Pair;
public class Test
{
static final Schema PAIR_SCHEMA;
static final String RECORD_STRING =
"{\n" +
" \"type\": \"record\",\n" +
" \"name\": \"exampleRecord1\",\n" +
" \"fields\": [\n" +
" {\"name\": \"id\", \"type\": \"string\"}\n" +
"]\n" +
"}";
static
{
final Schema.Parser parser = new Schema.Parser();
Schema KEY_SCHEMA = parser.parse( RECORD_STRING );
Schema VALUE_SCHEMA = Schema.create( Schema.Type.STRING );
PAIR_SCHEMA = Pair.getPairSchema( KEY_SCHEMA, VALUE_SCHEMA );
}
public static void main( String[] args )
{
System.out.println( PAIR_SCHEMA.toString(true) );
}
}
Here's the output:
{
"type" : "record",
"name" : "Pair",
"namespace" : "org.apache.avro.mapred",
"fields" : [ {
"name" : "key",
"type" : {
"type" : "record",
"name" : "exampleRecord1",
"namespace" : "",
"fields" : [ {
"name" : "id",
"type" : "string"
} ]
},
"doc" : ""
}, {
"name" : "value",
"type" : "string",
"doc" : "",
"order" : "ignore"
} ]
}