Formatting json properties - json

I have a JSON file with thousands of lines, which i need to have in another format.
My current code looks like that:
[{"Date":"2012-05-11","Value":19.5},
{"Date":"2012-05-15","Value":19.5},
{"Date":"2012-05-16","Value":18},
{"Date":"2012-05-17","Value":17.75},
...]
And it should look like this:
[Date.UTC(2012,5,11),19.5],
[Date.UTC(2012,5,13),19.5],
[Date.UTC(2012,5,16),18],
[Date.UTC(2012,5,17),17.75],
....]
Is there a tool or a quick way to do this, changing thousands of lines manually would take too much time.
Thanks in advance.

Programmatically, say php for instance, and treating each line of your JSON file as a string, you could use preg_replace(), but since it's a one time requirement, simply load your json file into a text editor and:
Delete the leading and ending square braces.
Find all {"Date":" replace with [Date.UTC(
Find all ","Value": replace with ),
Find all }, replace with ],
Save

Related

format a json key-value pair's first field

I have a json output I'd like to convert to a Zabbix parameter.
Basically, I need to transform something like the following:
"key":"value"
into something like this:
"#{KEY}":"value"
Leaving everything else untouched.
That is, I need to convert the first field to uppercase, surround it with square brackets and add a hash before the first bracket.
I've tried some combinations of sed, awk and grep, but things quickly got too long and messy to do so effectively.
I'm really unfamiliar with json and sparesly deal with it - I'd really appreciate any pointers.
Thanks!
EDIT: link below has solved this. To clarify, this is from a bash environment.

JSON Replace Text with variable

I have a JSON File in which are Dateobjects. I want to replace them with Formated Date Strings. Can somebody give me a hint how it would be possible in an easy way?
In the JSON File there are lines like:
"insertedAt": { "$date": "2018-01-31T11:05:39.447Z" },
I want to replace them with:
"insertedAt": "2018-01-31 11:05:39.447000"
The files are pretty big so I would like to avoid loading them into JSON-Objects and solve it with regex replacement or something similar. Unfortunatly I am not really experienced with it.
Is it possible to replace something with a function output of itself?
I can select the parts that I want to replace with the regex:
"insertedAt":{".*"}
Can I now replace it with a function of *? In Pseudocode it would be like:
replace("\"insertedAt\":{\".*\"}").with("\"insertedAt\":" + format(*))
Any other ideas?
If you can use lookbehind in your regex engine, then you can try this for matching target string
(?<=insertedAT\": )\{ \"\$date\": (\"[^\"]*\") \},
and replace it with capture group 1 ( \1 or $1). Demo
Or, if you can't use lookbehind in your regex engine, then you can try this regex,
(?!insertedAT\": )\{ \"\$date\": (\"[^\"]*\") \},
Demo

I need to remove a piece of every line in my json file

I have a json output on my notepad and i know it is not in the correct format. At the end of each line there is a time stamp which is causing the bad format. I want to get rid of it using find and replace since the file is pretty big. The format is as follows :
"eventtimestamp": "05 23 2017 04:01:02"}
The above piece comes in at the end of every line. How can i get rid of it using find a replace or any other way.
All help is appreciated.
Thank you
If you need to alter every line in a consistent way then regex find/replace is a good option. Free tools like atom.io, Notepad++, and plenty of others offer this feature.
Assuming "eventtimestamp" is constant, then a simple regex that says "find everything starting with "eventtimestamp" and up to a '}'" will work.
"eventtimestamp".*(?=})
And "replace" that with an empty string.
ps) here's a demo of the regex in regexr.com--hovering over the parts of the pattern will explain what they do.
If you are not sure that the eventtimestamp field always comes in at the end of a line and/or as the last element of the object, prefer that kind of pattern: "eventtimestamp":\s*"[^"]+",?.
Note the useful surrounded excepted character class pattern "[^"]+" that can be declined with any other delimiter.

Not able to load CSV file in weka

I am not being able to load csv file using weka, I have removed each and every special symbol even using text editor, still no luck. I am attaching the file, I will be obliged if solve this problem.
It shows "Wrong number of values, Read 31, expected 27, read token[EOL], line 3"
link : https://drive.google.com/open?id=0By7zyIPDD6HJMmthWnZLSUk5aFE
You have planty of empty fields in your file and if you download it as .csv even the header gets three commas at its end.
e.g. your 6th line:
,Doug Walker,,,131,,Rob Walker,131,,Documentary,Doug Walker,Star Wars: Episode VII The Force Awakens  ,8,143,,0,,,,,,,,,12,7.1,,0,,,
Simmilar to the suggestion in this post you could try s.th. like notepad++ or another text editor to replace ",," by ",?," to fill up your gaps.
Convert NA values to ? automatically while loading
I did this and then you get in your first row two question marks as column names wich obviously doesnt work, so change the first row to look like this:
color,director_name,num_critic_for_reviews,duration,director_facebook_likes,actor_3_facebook_likes,actor_2_name,actor_1_facebook_likes,gross,genres,actor_1_name,movie_title,num_voted_users,cast_total_facebook_likes,actor_3_name,facenumber_in_poster,plot_keywords,?,num_user_for_reviews,language,country,content_rating,budget,title_year,actor_2_facebook_likes,imdb_score,aspect_ratio,movie_facebook_likes,additionalColName1,additionalColName2,additionalColName3
if you try now to import your data weka starts telling you which lines it doesn't like and why. Btw. you did not "removed each and every special symbol"!
After removing a fiew lines with e.g. the Ç character it worked.
Thats just an ugly workaround, try filling the empty values and find a regular expression or a better way to save your file to remove the last three commas of every line, i was just too lazy for now. But i could load it into weka and that's what you wanted (:

In Stata, how do I add variable labels from a separate csv file?

I have a set of csv files that are very simple to load into Stata using the -insheet- command. But they have very uninformative variable names. For each of these files, I also have a file of metadata consisting of two columns: the original (uninformative) variable names, and a description of what the variables actually mean. I'd like to use these metadata files to create variable labels, preferably without going through and typing up all the separate label commands or turning the metadata file into a dictionary for each file. It seems like there must be a quick way of loading the metadata file into Stata and looping through it to generate the label commands, but I don't know what it is. Any thoughts?
Ideally each line of the metadata is something like
varname1 "more interesting description"
in which case you can prefix each line with
label var
and then run the file as if it were a do-file using do. See the help for label. That is easy in a decent text editor, as for example searching for the start of each line and replacing it with label var (note the need for the space).
What could bite here includes:
You don't have double quotes " " as delimiters, in which case you need to insert them.
The extra information does not qualify as a variable label because it is more than 80 characters long. See help limits.
There are other ways to do this with Stata. You could write a program to read in the metadata and write out a do-file using file, but if this were my problem I would reach first for my text editor. (Most experienced Stata programmers use something else as well as doedit.)