Is there any way to convert multiple JSON files into one CSV-file ?
My JSON file is like this:
{
"Title" : {
"name" : "ABC",
"id" : "1",
"job": "Teacher"
},
"Circle":{
"area":"2R"
},
"Triangle":{
"length":"45"
}
}
If you will see, this JSON has 3 roots and different elements under each root.
How to convert this JSON to CSV so that it can be opened in excel and can be viewed as follows:
Title
Name ABC
id 1
job Teacher
Circle
area 2r
Triangle
length 45
Can someone please suggest?
It would say that you should first parse you JSON with something like Jackson, and then you can write down a CSV file by using their extension. That's an option.
Other way might be to use an external tool to do the conversion, such as json2csv.
Hope it helps!
There is a library json2flat. It converted your json
{
"Title" : {
"name" : "ABC",
"id" : "1",
"job": "Teacher"
},
"Circle":{
"area":"2R"
},
"Triangle":{
"length":"45"
}
}
to the following
/Title/name,/Title/id,/Title/job,/Circle/area,/Triangle/length
"ABC","1","Teacher","2R","45"
Hope it helps. After all it depends upon users how they want to interpret it.
Related
I have a java program and i am parsing a json file. Because there are some dependencies between the json objects (they are procedures that must be executed so some of them depends on others). I want to make a graph so i can represent this. Is there any known way? I tryed mxgraph (jgraph) but i cannot make the representation.
Here is a simple json format
{
"blueprint":
{
"1" : { "depends" : null },
"2" : { "depends" : "1" },
"3" : { "depends" : { "2" , "1"} },
}
}
Answering this old question of mine just in case somebody needs it.
graphviz.org with dot language was the way I tackled it.
Thank you everyone for the comments.
I have some spring MVC based APIs which produces JSON/ XML which represents API output.
{
"data" :
{
"users": [
{
"id": "001",
"name: "abc1",
"type": {
"id": "P",
"name": "Permanent"
}
},
{
"id": "002",
"name: "xyz",
"type": {
"id": "C",
"name": "Contractor"
}
}
]
}
}
I'm passing a parameter with request as
url?fields=users.id, users.type.id
users.type.id is a sub-node in users node.
users node is an array.
Now, what I want to do is to filter those only properties and create the response based upon fields passed in the request.
So the response to above filter condition should be same structure and will only contain wanted fields with values.
I'm trying to build a flat map with keys with a dot notation so I won't lose the track to filter, then I'll rebuild the JSON again. I feel this approach is just unreasonable because Jackson has the .path and .with API's to check existing path. But the real challenge is to extract and create a new JSON which matches the response JSON.
I'm looking for some ideas to achieve this. I don't want to try any third party libs btw. I know some libs are there. I want to prefer Jackson way to do this.
Feel free to add or comment if you have a further ideas.
I have a JSON structure, which is small.
My requirement is to expand the JSON structure.
Example JSON structure :
'{
"CallLog":{
"Three":{
"age":100,
"name":"Sample",
"Other":100,
"Add":"Sample"
},
"One":{
"CallLogEntry":[
{
"ContryCode":{
"CountryCode":123
}
},
{
"Phone Number":{
"PhoneNumber":456
}
},
{
"Name":{
"name":456
}
}
]
},
"Two":{
"age":100,
"name":"Sample",
"Other":100,
"Add":"Sample"
}
}
}
So, i want to expand his JSON.
Lets say i want repeat "One" node 10 times, and "two" node 5 times, and then write this expanded JSON in the new JSON.
How can i do this?
If you want to do it through java, i would recommend converting this JSON to java code first.Once you get the corresponding classes e.g. CallLog.java,CallLogEntry.java etc , you can define a new Class CallLog1.java and declare 10 'One' , 5 'Two' etc , as per your requirement and then create the JSON object. You can copy paste your json to http://www.jsonschema2pojo.org/ to get POJO , and then create you own POJO , and convert it into JSON , using library like GSon.
I wanted to create a service which can accept JSON as an input to my endpoint. eg.
[{ "name": "Ram","event": "processed" },
{"name": "Raj", "event": "click" }] .
Tried some of the possible ways, cloud endpoints are not supporting arrays or any array related types to use. I tried it to convert the json to string but it's not working for me.
Thanks in advance.
You seem to have an invalid JSON. Try validating it here. As you can see, you will encounter a parsing error.
Change all occurrences of “ with "
[
{
"name": "Ram",
"event": "processed"
},
{
"name": "Raj",
"event": "click"
}
]
and now you have a valid JSON.
More details on JSON can be found here.
I have a json string like this:
"files": {
"fileA.c": {
"size": 100
},
"fileB.txt": {
"size": 200
}
}
I want to extract the file names, {"fileA.c","fileB.txt"}, using JsonPath. Note that the number of files is unknown.
The problem is, I don't know whether the file name is a key or a value:
If it is a key...well I certainly don't know the key name because that's the information I want to extract.
If it is a value, then what is its key?
Can I use JsonPath to extract the file names? If so, how?
If JsonPath cannot do this, is there any Java library for Json that can achieve this?
In your example, fileA.c and fileB.txt are keys, you can get them by iterating on the key in the enclosing object (which is referenced by the key "files").
I don't think JSONPath is really appropriate (or even applicable) in this case, it is designed to access elements when you know the structure of the documents, which means that basically you know the keys. It would be much easier to simply use a JSON parser.
I would suggest you to modify your data structure, to something like this;
var data = {
"files": [
{
"name": "fileA.c",
"size": 100
},
{
"name": "fileB.txt",
"size": 200
},
{
"name": "fileC.txt",
"size": 50
}
]
};
When structured like this, you can use DefiantJS (http://defiantjs.com) to query for files which sizes are larger than 100...like this:
JSON.search(data, '//*[size >= 100]/name')
DefiantJS extends the global object JSON with the method "search", with which you can query JSON structure with XPath expressions.
To see a working example with your data, check out this fiddle:
http://jsfiddle.net/jRN22/