0

I was in a meeting yesterday where I was presenting a program that referenced a certain json file with data like this:

[
{
  id: 1,
  text: '...',
  ...
},
{
  id: 2,
  text: '...',
  ...
}
]

In total there were around 6 of these objects in the array. I was asked how the program would handle a set of 100 objects. I tried simply copying the objects several times, then realized that this would be useless for my program unless the id incremented for each object. Of course, I could write something in you-name-it programming language to create such a json file, but I suspected there must be some way of using an emacs macro to quickly generate such a list of objects on the fly. Is this so? What's the most emacs-y way of doing this?

1

You can just make a list and then encode it as json like this:

(require 'cl-lib)
(require 'json)
(json-encode (cl-loop for i to 100 collect (list (cons 'id i) (cons 'text "..."))))

That gives me this string:

[{"id":0,"text":"..."},{"id":1,"text":"..."},{"id":2,"text":"..."},{"id":3,"text":"..."},{"id":4,"text":"..."},{"id":5,"text":"..."},{"id":6,"text":"..."},{"id":7,"text":"..."},{"id":8,"text":"..."},{"id":9,"text":"..."},{"id":10,"text":"..."},{"id":11,"text":"..."},{"id":12,"text":"..."},{"id":13,"text":"..."},{"id":14,"text":"..."},{"id":15,"text":"..."},{"id":16,"text":"..."},{"id":17,"text":"..."},{"id":18,"text":"..."},{"id":19,"text":"..."},{"id":20,"text":"..."},{"id":21,"text":"..."},{"id":22,"text":"..."},{"id":23,"text":"..."},{"id":24,"text":"..."},{"id":25,"text":"..."},{"id":26,"text":"..."},{"id":27,"text":"..."},{"id":28,"text":"..."},{"id":29,"text":"..."},{"id":30,"text":"..."},{"id":31,"text":"..."},{"id":32,"text":"..."},{"id":33,"text":"..."},{"id":34,"text":"..."},{"id":35,"text":"..."},{"id":36,"text":"..."},{"id":37,"text":"..."},{"id":38,"text":"..."},{"id":39,"text":"..."},{"id":40,"text":"..."},{"id":41,"text":"..."},{"id":42,"text":"..."},{"id":43,"text":"..."},{"id":44,"text":"..."},{"id":45,"text":"..."},{"id":46,"text":"..."},{"id":47,"text":"..."},{"id":48,"text":"..."},{"id":49,"text":"..."},{"id":50,"text":"..."},{"id":51,"text":"..."},{"id":52,"text":"..."},{"id":53,"text":"..."},{"id":54,"text":"..."},{"id":55,"text":"..."},{"id":56,"text":"..."},{"id":57,"text":"..."},{"id":58,"text":"..."},{"id":59,"text":"..."},{"id":60,"text":"..."},{"id":61,"text":"..."},{"id":62,"text":"..."},{"id":63,"text":"..."},{"id":64,"text":"..."},{"id":65,"text":"..."},{"id":66,"text":"..."},{"id":67,"text":"..."},{"id":68,"text":"..."},{"id":69,"text":"..."},{"id":70,"text":"..."},{"id":71,"text":"..."},{"id":72,"text":"..."},{"id":73,"text":"..."},{"id":74,"text":"..."},{"id":75,"text":"..."},{"id":76,"text":"..."},{"id":77,"text":"..."},{"id":78,"text":"..."},{"id":79,"text":"..."},{"id":80,"text":"..."},{"id":81,"text":"..."},{"id":82,"text":"..."},{"id":83,"text":"..."},{"id":84,"text":"..."},{"id":85,"text":"..."},{"id":86,"text":"..."},{"id":87,"text":"..."},{"id":88,"text":"..."},{"id":89,"text":"..."},{"id":90,"text":"..."},{"id":91,"text":"..."},{"id":92,"text":"..."},{"id":93,"text":"..."},{"id":94,"text":"..."},{"id":95,"text":"..."},{"id":96,"text":"..."},{"id":97,"text":"..."},{"id":98,"text":"..."},{"id":99,"text":"..."},{"id":100,"text":"..."}]
  • Did you mean (require 'json)? I can't check right now, but I believe json isn't loaded by default. (And also, I think cl-loop comes from cl-lib, not cl.) – Omar Feb 7 at 22:25
  • Thanks for pointing out the json require. It seems to work with requiring either cl or cl-lib. I have modified the answer to include the json require, and to use cl-lib. – John Kitchin Feb 8 at 13:08
  • For versions of Emacs where cl-lib is built-in (24.3+ I believe), cl is implemented on top of cl-lib (and therefore require's it). In earlier Emacs versions where cl-lib is installed as a package, you wouldn't get that. – npostavs Feb 8 at 14:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.