2

I am trying to parse json output from neo4j in java as:

Object obj = parser.parse(new FileReader("D:\\neo4j.json"));

JSONArray json = (JSONArray)  obj;

System.out.println(json.size());

for (int i = 0; i < json.size(); i++) {
    JSONObject jsonObject = (JSONObject) json.get(i);
    String data = (String);   
    jsonObject.get("outgoing_relationships");
    String name = (String) jsonObject.get("name");
    System.out.println(data);
    System.out.println(name);       
}

Can somebody help me to get values inside "data" element:

I have json output from neo4j as follows:

[{
"outgoing_relationships": "http://host1.in:7474/db/data/node/133/relationships/out",
"data": {
    "MOTHERS_NAME": "PARVEEN BAGEM",
    "MOBILE_NO": "9211573758",
    "GENDER": "M",
    "name": "MOHD",
    "TEL_NO": "0120-",
    "PINCODE": "110001"
},
"traverse": "http://host1.in:7474/db/data/node/133/traverse/{returnType}",
"all_typed_relationships": "http://host1.in:7474/db/data/node/133/relationships/all/{-list|&|types}",
"property": "http://host1.in:7474/db/data/node/133/properties/{key}",
"self": "http://host1.in:7474/db/data/node/133",
"properties": "http://lhost1.in:7474/db/data/node/133/properties",
"outgoing_typed_relationships": "http://host1.in:7474/db/data/node/133/relationships/out/{-list|&|types}",
"incoming_relationships": "http://host1.in:7474/db/data/node/133/relationships/in",
"extensions": {

},
"create_relationship": "http://host1.in:7474/db/data/node/133/relationships",
"paged_traverse": "http://host1.in:7474/db/data/node/133/paged/traverse/{returnType}{?pageSize,leaseTime}",
"all_relationships": "http://host1.in:7474/db/data/node/133/relationships/all",
"incoming_typed_relationships": "http://host1.in:7474/db/data/node/133/relationships/in/{-list|&|types}"
}]

Regards, Jayendra

2 Answers 2

1

You can try following way. Inside the for loop get the data node as JSONObject. From that data node you can extract every property. I just extracted mother name from data.

JSONObject data = (JSONObject) jsonObject.get("data");
final String motherName = (String) data.get("MOTHERS_NAME");
Sign up to request clarification or add additional context in comments.

Comments

0

What library are you using to parse JSON ? I'd recommend that you use Jackson

For eg: To get the data you read from the file in a Map, you can write a method like this.

   @SuppressWarnings("rawtypes")
 public static Map toMap(Object object) throws JsonProcessingException{                   ObjectMapper mapper = new ObjectMapper();
    return mapper.convertValue(object, Map.class);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.