Hello Shweta,
Where did you get that JSON? I ask because it's not valid. I put it in a
JSONPath evaluator[1] and cleaned it up:
{
"item":[
{
"tags": ["Java","Hadoop","nimbus"],
"id": "2233",
"title": "testing with Java",
"comments":[{"post_id":"2233","body":"try option1"} ,
{"post_id":"2233","body":"try option2"} , {"post_id":"2233","body":"tryoption3"}
]
},
{
"tags": ["Java","Hadoop"],
"id": "2232",
"title": "testing with Java",
"comments":[{"post_id":"2232","body":"try option1"} ,
{"post_id":"2232","body":"try option2"} , {"post_id":"2232","body":"tryoption3"}
]
},
{
"tags": ["Java"],
"id": "2231",
"title": "testing with Java",
"comments":[{"post_id":"2231","body":"try option1"} ,
{"post_id":"2231","body":"try option2"} , {"post_id":"2231","body":"tryoption3"}
]
}
]
}
You were missing the proper beginning and some commas.
As for "iterating" over them you want to use the "*" path operator like such:
$.item.*.id
This path returns the ids for each item:
'0' => "2233"
'1' => "2232"
'2' => "2231"
[1] http://jsonpath.com/
Let me know if you have any other issues,
Joe
- - - - - -
Joseph Percivall
linkedin.com/in/Percivall
e: [email protected]
On Monday, December 14, 2015 12:57 PM, shweta <[email protected]> wrote:
Hi All,
I have a JSON which looks as following:-
item[{
"tags": ["Java","Hadoop","nimbus"]
"id": "2233"
"title": "testing with Java"
"comments":[{"post_id":"2233","body":"try option1"} ,
{"post_id":"2233","body":"try option2"} , {"post_id":"2233","body":"try
option3"}]},
{
"tags": ["Java","Hadoop"]
"id": "2232"
"title": "testing with Java"
"comments":[{"post_id":"2232","body":"try option1"} ,
{"post_id":"2232","body":"try option2"} , {"post_id":"2232","body":"try
option3"},
{
"tags": ["Java"]
"id": "2231"
"title": "testing with Java"
"comments":[{"post_id":"2231","body":"try option1"} ,
{"post_id":"2231","body":"try option2"} , {"post_id":"2231","body":"try
option3"}
]
I need to convert the JSON to CSV.
Id Tags Title
Body
2233 , <java><Hadoop><Nimbus> , "testing with Java" , <"try
option1"><"try option2"><"try option3">
2232 , <java><Hadoop> , "testing with Java" , <"try option1"><"try
option2"><"try option3">
.
.
I used a combination of EvaluateJSONPath and Replace Text for the same.
First issue I'm facing is in parsing an array (Tags and Body). I couldn't
figure out how can iterate over the array of JSON values.
For Simplicity sake I configured EvaluateJSONPath with values shown in the
image:-
<http://apache-nifi-developer-list.39713.n7.nabble.com/file/n5776/EvaluateJsonPath.png>
and Replace Text processor with
Regex expression as [\S\s]+ and Replacement Values as
"${Body}","${Tags},"${Post_id}","${Title}".
It works for single record.
Can you please provide pointer how to iterate through value of arrays.
Thanks,
Shweta
--
View this message in context:
http://apache-nifi-developer-list.39713.n7.nabble.com/How-to-iterate-through-complex-JSON-objects-tp5776.html
Sent from the Apache NiFi Developer List mailing list archive at Nabble.com.