Skip to content
Advertisement

Replace each reoccuring string value of a one line flat json to a random value using python

I have a JSON file (input.json) like the following which has 2 rows… (The real one has more than 1m rows)

JavaScript

I want to basically change the value of each and every field with name four to a random value, so basically wherever it appears, it will remove what it currently have and change it to a random given value:

expected output:

JavaScript

It could be any random value..

I’m kinda struggling with python at the moment, any tips or solutions would really help!

JavaScript

Advertisement

Answer

You need to change your input file structure, the elements must be on a list, like this:

JavaScript

You can change a key value with this:

JavaScript

choices method will randomly choose 10 characters from ‘asdf1234’, join will create a single string, and the result will be store into four key.

Advertisement