I'm able to parse json files in MFC but is having a hard time modifying the values. Is there an easier way writing new values, other than converting it to native file types, modifying the contents and converting it back to json again?
I thought it would be as easy as changing values in an XML file where you just look for the tag and change it's value.
thanks...
You can use JSON Spirit library. The way it traverses through the json file is through it's key and value which is treated as a "pair". All you have to do is loop through the objects and search for the pair you want to replace. That's it...
The details aren't shown here, but pretty much gives you the basics -> http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx. It's got a bunch of methods you could use for whatever operation you want.
:)
Related
Hi I'm trying to parse any of the files from the link underneath. I've tried reaching out to the owner of the data dumps, but nothing works in trying to parse the files as proper JSON files. No program we use (Power BI, Jupyter, Excel) anything really, wants to recognise the files as JSON and we can't figure out why this might be. I was wondering if anyone could help figuring out what the issue is here as this dataset is very interesting to me and my co-students. I hope I'm using the word 'parsing' correctly.
The link to the data dumps is linked underneath:
https://files.pushshift.io/reddit/comments/
The file I downloaded (I just tried one at random) was handled just fine by jq, my preferred command-line tool for processing JSON files.
jq accepts an input consisting of a sequence of JSON objects, which is what I found when I decompressed the test file. This format is commonly known as JSON lines, and many tools can handle it. The Wikipedia article on JSON streaming contains more information and a (possibly outdated) list of tools.
If your tools aren't capable of handling more than one JSON object in an input, you could turn the files into something which you can handle by adding a comma to the end of every line except the last one (since each JSON object is a single line) and then surrounding the whole input inside a pair of brackets to turn the sequence into a JSON list. Since JSON does not actually care about newlines, it would be sufficient to add a line containing [ at the beginning and a line containing ] at the end. I don't know what command-line tools you have available and are comfortable with, but the task shouldn't be too difficult.
I write this code and it append my json file with all data again.
After this content of my file become twice.
QFile file("C:/Users/Vizexperts/Documents/QT projects/Json_File_Read_Write/new 1.json");
file.open(QIODevice::ReadWrite|QIODevice::Text);
QString dataString=file.readAll();
QJsonDocument doc= QJsonDocument::fromJson(dataString.toUtf8());
QJsonObject dataobject =doc.object();
qDebug()<<(dataobject)["D"].toString();
QJsonValue valueA=dataobject.value(QString("A"));
QJsonObject valueAobject=valueA.toObject();
QJsonValue valueA1=valueAobject["A1"];
qDebug()<<valueA1.toString();
ui->label->setText(valueA1.toString());
dataobject["D"]="a";
doc.setObject(dataobject);
file.write(doc.toJson());
file.close();
There is no easy way to edit a single value in a JSON file and then writing it back.
The easiest (in terms of prone to errors) way is to
Open file
Load text into JSON object
Edit data
Write JSON object back to file
When you open a file for writing and want to overwrite the existing data in it, you need to also use the QIODevice::Truncate flag. This truncates the text inside your JSON file and whatever you write to that file erases the previous content.
If you want, you can open the file twice (and close it accordingly) - once when you read from it (so no writing access to it) and once when you write to it. This way you can handle the given operation's specific errors that may occur. If error handling is not something you want to bother yourself with, just add the extra flag and you are good to go.
It appears you are rewriting the JSONdoc to file.
file.write(doc.toJson());
As far as I know there is no way to just update the file with the change you did in the code. The easy way is to clear the file and rewrite the whole object again.
A bigger challenge would be to parse the file again, compare the JSON objects, find the cursor position where you need to write the changes and update the file. Depending on the size of the JSON object, this seems over-engineered.
I went through previous posts on SO and some of the answers say that a JSON file is used to send data from server to client.
Well that seems to be okay but then we can create package.json, Apidoc.json, manifest.json which do not interact with the client and server
So can someone tell me what actually is a JSON file?
JSON stands for JavaScript Object Notation. It is used to describe a data structure in a simple format. It can be a plain text file, which may be used to pass data from the server to a client, but it could be equally used to hold and consume that data at the same layer e.g. you could have a configuration file at the client side which is read an interpreted by your application.
Note also that JSON does not need to be held in a file; you could create a string variable with JSON data in it and pass this from one method to another without ever storing it in a file.
The tag definition in Stack Overflow can be found here https://stackoverflow.com/tags/json/info and further information can be found here https://www.json.org/.
JSON is a file format, just like CSV. Just because CSV is used with Microsoft Excel, does not mean that is all it is used for (just like with JSON). Just because it is common to get info from a server in JSON format, does not mean that is all JSON is used for. Do some googling before asking a question like this on Stack Overflow.
Here is an intro to JSON. JSON Intro W3Schools
I'm working on an ETL job that will ingest JSON files into a RDS staging table. The crawler I've configured classifies JSON files without issue as long as they are under 1MB in size. If I minify a file (instead of pretty print) it will classify the file without issue if the result is under 1MB.
I'm having trouble coming up with a workaround. I tried converting the JSON to BSON or GZIPing the JSON file but it is still classified as UNKNOWN.
Has anyone else run into this issue? Is there a better way to do this?
I have two json files which are 42mb and 16mb, partitioned on S3 as path:
s3://bucket/stg/year/month/_0.json
s3://bucket/stg/year/month/_1.json
I had the same problem as you, crawler classification as UNKNOWN.
I were able to solved it:
You must create custom classifier with jsonPath as "$[*]" then create new crawler with the classifier.
Run your new crawler with the data on S3 and proper schema will be created.
DO NOT update your current crawler with the classifier as it won't apply the change, I don't know why, maybe because of classifier versioning AWS mentioned in their documents. Create new crawler make them work
As mentioned in
https://docs.aws.amazon.com/glue/latest/dg/custom-classifier.html#custom-classifier-json
When you run a crawler using the built-in JSON classifier, the entire file is used to define the schema. Because you don’t specify a JSON path, the crawler treats the data as one object, that is, just an array.
That is something which Dung also pointed out in his answer.
Please also note that file encoding can lead to JSON being classified as UNKNOWN. Please try and re-encode the file as UTF-8.
I have a problem with JSON format.
I am going to generate a 2D building plan in JSON format automatically. Then this JSON file is loaded to Three.JS for visualization.
Currently, I can easily create various geometry type in JSON format based on:
https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3.1
However, I need to know more about this data format.
Firstly, how can I add text to the model?
Secondly, I could not find line as a geometry in this format. Is there?
Thirdly, is there any document that I can refer to it for more explanation regarding this
format?
Lastly, how can I add additional information about geometries
inside the JSON format? for example when I create a polygon in JSON
format, I want to add additional information regarding that polygon
such as area, owner,...
Either through a texture, or by creating THREE.TextGeometry and merging that with the other geometry
AFAIK, no. You might be able to emulate lines by loading a separate object that you render as wireframe.
I've used the source code: Loader, JSONLoader
I don't think that's possible while maintaining compatibility, i.e. you would need to patch Three.js loader.
One option for achieving what you want could be to add custom entries to the JSON, e.g. "lines": [], "customFaceProperties": [] and then parse them yourself once the stock Loader has parsed everything it understands.
Also, since it seems you might be creating something that has different things in it, you might want to take a peek at the scene loading capabilities: example, SceneLoader.js