Modify json file with ruby

Modify json file with ruby



I have a json file that I need to access and modify with a ruby script.
I know how to open a json, how to write a new one, but can I modify an existing one?



I have searched for it a bit, but I have not found anything helpfull yet..
Only results treats on different programming languages ..



exemple : I want to modify a wrong data, like below, Anna's last name.



employee.json


"employees":[
"firstName":"John", "lastName":"Doe",
"firstName":"Anna", "lastName":"Smith",
"firstName":"Peter", "lastName":"Jones"
]



=>


"employees":[
"firstName":"John", "lastName":"Doe",
"firstName":"Anna", "lastName":"David",
"firstName":"Peter", "lastName":"Jones"
]



Thanks in advance,





Read the json file into a ruby structure (hash, in this case). Modify the ruby structure. Write it out to the file. PROFIT.
– Sergio Tulentsev
Jul 1 '15 at 8:12





Yeah, but does it still work for a large file? (~15k lines)
– Sokrah
Jul 1 '15 at 9:14






Why wouldn't it work?
– Sergio Tulentsev
Jul 1 '15 at 9:22





The point is, I was afraid that work on a large file would require a lot of capacity. But, as shivam explained clearly, that is still the best way to go when you look at the complexity. Whatever, i'll go for that way ! Thanks
– Sokrah
Jul 1 '15 at 9:35




2 Answers
2



Convert json to hash, edit hash, convert back to json:


require 'json'

a = '"employees":[
"firstName":"John", "lastName":"Doe",
"firstName":"Anna", "lastName":"Smith",
"firstName":"Peter", "lastName":"Jones"
]'

# Converting JSON to Hash
hash = JSON.parse a
# => "employees"=>["firstName"=>"John", "lastName"=>"Doe", "firstName"=>"Anna", "lastName"=>"Smith", "firstName"=>"Peter", "lastName"=>"Jones"]

# Modifying Hash as required
hash["employees"][1]["lastName"] = "David"

# Modified Hash
hash
# => "employees"=>["firstName"=>"John", "lastName"=>"Doe", "firstName"=>"Anna", "lastName"=>"David", "firstName"=>"Peter", "lastName"=>"Jones"]


# Converting Hash back to JSON
hash.to_json
# ""employees":["firstName":"John","lastName":"Doe", "firstName":"Anna","lastName":"David", "firstName":"Peter","lastName":"Jones"]"



I have directly modified the hash as I can see the exact index and iterating through Hash was not the question. But in real world example you may want to go through the Hash to look for key and then modify it, instead of doing to directly as in above example.



You can use pretty_generate to pretty print your json. Here:


pretty_generate


hash
# => "employees"=>["firstName"=>"John", "lastName"=>"Doe", "firstName"=>"Anna", "lastName"=>"David", "firstName"=>"Peter", "lastName"=>"Jones"]

puts JSON.pretty_generate hash
#
# "employees": [
#
# "firstName": "John",
# "lastName": "Doe"
# ,
#
# "firstName": "Anna",
# "lastName": "David"
# ,
#
# "firstName": "Peter",
# "lastName": "Jones"
#
# ]
#





As simple as that... Thank you ! But , does it still be efficiency with large json file? (~ 15k lines)
– Sokrah
Jul 1 '15 at 9:16





operation on Hash is O(1). Which is magnitudes more efficient than alternate options like reading line by line and editing the file.
– shivam
Jul 1 '15 at 9:19






Couldn't expect a clearer answer. Thanks
– Sokrah
Jul 1 '15 at 9:32



jsonpath gem can do it out of the box


JsonPath.for('"candy":"lollipop"').gsub('$..candy') .to_hash



in your case something like


require 'jsonpath'

a = '"employees":[
"firstName":"John", "lastName":"Doe",
"firstName":"Anna", "lastName":"Smith",
"firstName":"Peter", "lastName":"Jones"
]'
JsonPath.for(a).gsub('$.employees[1].lastName') "David" .to_hash



will do the trick



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)