Sort fields in rendered JSON - spray-json

Using Spray JSON, how can I render the objects with their fields alphanumerically sorted?
My objective is to get sorted output so that I can use text-only diff tools.

In the latest version of spray-json there is now a sortedPrint method for this: https://github.com/spray/spray-json/pull/164

Related

How can I bold part of a string from JSON in Swift?

I have a JSON file like this. I have to make bold part of string which is shown in JSON. How can I make parse this JSON?
It looks to me like you would first want to use NSJSONSerialization (Or just JSONSerialization in Swift 3) to convert your JSON to an object graph. Once you've done that, you should be able to navigate to the interestLabel keys in your data and fetch those strings.
You'll then need to parse those tagged strings somehow. If the only thing you need to do is to find <b> and </b> bold tags, and no other tags will ever appear in your data then you could probably write your own code. If the strings might have other tags and/or more complex HTML structure then you might want to use an XML/HTML parser. I suggest taking a look at this tutorial: https://www.raywenderlich.com/14172/how-to-parse-html-on-ios

Accessesing saved JSON response in column

Okay, I am using HTTParty to save a JSON response in my show.subsources column. Subsources is a text type column.
For example, one of my subsources: column currently has the data saved as so:
[{"source"=>"hulu_free", "display_name"=>"Hulu", "id"=>6001348,"link"=>"http://www.hulu.com/watch/843378"}]
To me, this looks like a hash within an array. How do I access each hash within the array, so that I can correctly display the "source" name in my views?
What I have tried is:
<% showdata = show.subsources %>
<%= showdata[0]['sources'] %>
I am expecting that to display 'hulu_free', but instead it will not show anything in my views. Am I not using the right syntax to access that hash? Or am I not saving the JSON correctly in order for me to access the hash data? Do I need to parse the data first? I have spent way too long trying to figure this out.
In your case you can easily get JSON representation and parse it to Hash.
text = text = %q{[{"source"=>"hulu_free", "display_name"=>"Hulu", "id"=>6001348,"link"=>"http://www.hulu.com/watch/843378"}]}
text.gsub!('=>',':')
JSON.parse(text) # => returns hash
I'm not sure how internals work in your program, but probably you should consider to put JSON like text into this field.

Format dart code as html

I am knocking together a quick debugging view of a backend, as a small set of admin HTML pages (driven by angulardart, but not sure that is critical).
I get back from my XHR call a complex JSON object. I want to see that on the HTML page formatted nicely. It doesn't have to be a great implementation, as its just a debug ui, but the goal is to format the object instead of having it be one long string with no newlines.
I looked at trying to pretty print JSON in dart then putting that inside <pre></pre> tags, as well as just dumping the dart Map object to string (again, inside or not inside <pre></pre> tags. But not getting to where I want.
Even searched pub for something similar, such as a syntax highlighter that would output html, but didn't find something obvious.
Any recommendations?
I think what you're looking for is:
Format your JSON so it's readable
Have syntax highlight
For 1 - This can be done with JsonEncoder with indent
For 2 - You can use the JS lib called HighlightJs pretty easily by appending your formatted json into a marked-up div. (See highlightjs' doc to see what I mean)

Rails display data from XML file via respond_to format.json

I have an XML file containing text data that I need to display to the user. I am using ember.js and therefore need to provide my response in json format.
My initial thoughts are to load the XML file and convert it to json using XSLT and then rendering this. However, I don't fully understand how the respond_to format.json method works. At a guess, I would say it turns the result of the instance variable into json, so if my data is already in json would it cause me any issues using this approach?
What is the best way to render my XML file to a view in json?
If you want to combine json and xml features, you might use rabl:
https://github.com/nesquena/rabl

How to format a date to JSON in Rails?

I need to produce a date in Rails which looks like this:
/Date(1294268400000)/
I have tried various combinations of DateTime, to_i, to_json but never managed to get the /Date()/ thing.
Do I have to simply get my date in ms and then wrap the /Date(and )/ manually, or is there a built in method?
What about (ruby 1.9.x)?:
Time.now.strftime("/Date(%s%L)/")
=> "/Date(1335280866211)/"
You should try
new Date(posixMillisecondsHere)
first. MDN says that calling the Date function outside of the constructor context (i.e., without the new) will always return a string containing a formatted date rather than a Date object.
Strictly speaking, when you do that, you are writing JavaScript and not JSON. JSON cannot contain Date objects.
RFC 4627 says
2.1. Values
A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:
false null true
If you want to put a Date into what is strictly considered JSON and then get it back out, you must choose some way of using the JSON primitives (to wit, objects, arrays, numbers, strings, etc.) to encode a Date.
If you want to get a Date back out of JSON, whatever parses your JSON must understand the convention that you used to encode the Date.
Hope these are credible and/or official enough to help.
What about something like this:
in your config/en.yml file:
en:
time:
formats:
json: "/Date(%s%L)/"
and than in the view:
<%= l(Time.now, :format => :json) %>
Please note that you would need access to the helpers in the method that renders json. So it won't work if you are using ActiveRecord#to_json method for generating jsons.
Check out this question:
c# serialized JSON date to ruby
... simple answer seems to be to create a parse_date method.
It's the UNIX Epoch (seconds since 1970-01-01) right? What about using DateTime#strftime method?
# Taken from the Ruby documentation
seconds_since_1970 = your_date.strftime("%s")
UPDATE: OK, it's milliseconds, according to the documentation you can use your_date.strftime("%Q") to get the ms (but I've not tried yet).

Resources