quda

quda

Function accepting only json as argument, with guard?

Is it possible to create a function to accept only a valid json as parameter/argument ? Not map!
Precisely, I need something that would perform as:

def myFn(obj) when is_json(obj) do
...function content...
end

I am aware that is_json() does not exist in Elixir.
The idea is the function should accept only a string that will parse to json:
myFn("{\"k1\":7,"\k2\":"\hello\"}") should be valid but myFn("bla bla bla") not. (the json objects arrive already stringified from an upstream API)

Most Liked

l00ker

l00ker

I understand your frustration, but I just have to ask the question:

What modern language has native support for JSON?

  • JavaScript has JSON.parse & JSON.stringify to decode & encode JSON via the std library

  • Python has json.loads & json.dumps to decode & encode JSON via import json

  • PHP has json_decode & json_encode to decode & encode JSON via the std library

  • Ruby has JSON.parse & JSON.generate to decode & encode JSON via require 'json'

  • … [1]

The point I’m trying to make is that there doesn’t appear to be a modern language that has – what I would be willing to call – native support for JSON.

Every widely used (modern?) language has functions or methods to encode & decode its native objects or data structures to/from JSON.

Even JavaScript (the ‘J’ in JSON) doesn’t appear to have native support.

JSON (JavaScript Object Notation) is a lightweight data-interchange format [1], and in being such, I believe every language would need to attempt to parse JSON order to determine whether it’s valid or not, and Elixir is no exception. :slight_smile:

  1. json.org
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Guard clauses map to VM level instructions, you can only use those specific clauses in guards, in combinations. The only way to know that a string is JSON is to parse it, and I don’t see how you could create a set of guard clauses that would parse JSON.

hauleth

hauleth

That is not true. Few aren’t constant time:

  • length/1 is linear
  • is_map_key/2 and map access (map_get/2) are logarithmic (and dependants like is_struct/{1,2}
Nicd

Nicd

Yes, lists are singly linked lists and must be iterated to the end to find out the length. That’s why list == [] is recommended over length(list) == 0.

cmo

cmo

Use Jason.decode! to throw or Jason.decode and pattern match on the result. Not in a guard though.

Or similar in whatever json library you’re using.

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement